以前的结构是:
City collection with documents like: {_id, name, buildings:[ _ids from Building collection ]}
Building collection with documents like: {_id, name}
并转向:
City collection with documents like: {_id, name, buildings:[ {_id, name}, {_id, name}...]}
现在我的问题是:“buildings”sufield 中的 building jsons 中的 _id 曾经由 mongodb 生成,我只是将它插入到相应城市文档的“buildings”数组中。
但是现在,当我创建一个建筑物时,我直接将它插入到数组中。但是我仍然需要客户端对每个单独的建筑物进行某种识别,但我很困惑如何做到这一点。
我正在考虑在城市文档中保存一个数值,每次插入建筑物时都会增加该数值,并为其提供该 ID。这样,它将是该特定城市独有的。因此,所有具有一栋默认建筑物的城市都将拥有该建筑物的 id 为 0。
有没有更聪明/更优雅的解决方案?
PS:我还想轻松访问每个单独的建筑物,您是否也认为我应该将它们作为关联数组/对象保存,键为 id?