MongoDB / Node:将集合中的文档的一部分插入另一个集合中的文档

谢谢阅读。 我正在试图获取我存储在mongodb数据库中的大量数据,以便在NodeJS中使用mongoose模块显示在前端。 我想知道解决这个问题的最好方法是什么? 我的数据看起来像这样…

collections1:洞穴

{ "_id" : ObjectId("564d2f6eb0896138247ff791"), "name" : "ACME Cave", "slug" : "acme-cave", "timeCreated" : ISODate("2015-11-19T02:09:50.492+0000"), "address" : { "county" : "Somewhere", "state" : "CA", "country" : "USA" } } 

collections2:地点

 { "_id" : ObjectId("564d2f6fb0896138247ff855"), "latitude" : 54.5793621, "longitude" : -74.9669167, "parent_id" : ObjectId("564d2f6eb0896138247ff791"), <-- matches collection 1 "timeCreated" : ISODate("2015-11-19T02:09:51.413+0000"), } { "_id" : ObjectId("564d2f6fb0896138247ff855"), "latitude" : 48.5783611, "longitude" : -72.9669167, "parent_id" : ObjectId("564d2f6eb0896138247ff791"), <-- matches collection 1 "timeCreated" : ISODate("2015-11-19T02:09:51.413+0000"), } 

我想合并一些儿童领域的父母像这样…

 { "_id" : ObjectId("564d2f6eb0896138247ff791"), "name" : "ACME Cave", "slug" : "acme-cave", "timeCreated" : ISODate("2015-11-19T02:09:50.492+0000"), "address" : { "county" : "Somewhere", "state" : "NY", "country" : "USA" }, locations: [ { "_id" : ObjectId("564d2f6fb0896138247ff855"), "latitude" : 54.5793621, "longitude" : -74.9669167, "parent_id" : ObjectId("564d2f6eb0896138247ff791"), <-- matches doc collection 1 "timeCreated" : ISODate("2015-11-19T02:09:51.413+0000"), }, { "_id" : ObjectId("564d2f6fb0896138247ff855"), "latitude" : 48.5783611, "longitude" : -72.9669167, "parent_id" : ObjectId("564d2f6eb0896138247ff791"), <-- matches doc collection 1 "timeCreated" : ISODate("2015-11-19T02:09:51.413+0000"), } ] } 

我有下面这个片段完成这个,但它似乎并不正确。 有没有人有任何洞察到这样做的权利?

 Parent.find({}, {'locations': 1}).stream().on('data', function(parent) { // add the location to the parent doc Child.find({parent_id: {$in: parent._id}}, {'latitude': 1, 'longitude': 1}, function(err, child) { parent.locations = child; console.log(parent); }); }); 

这将返回下面的JSON,和我的输出一样。

 { "_id" : ObjectId("564d2f6eb0896138247ff791"), "name" : "ACME Cave", "slug" : "acme-cave", "timeCreated" : ISODate("2015-11-19T02:09:50.492+0000"), "address" : { "county" : "Somewhere", "state" : "NY", "country" : "USA" }, locations: [ { "_id" : ObjectId("564d2f6fb0896138247ff855"), "latitude" : 54.5793621, "longitude" : -74.9669167, "parent_id" : ObjectId("564d2f6eb0896138247ff791"), <-- matches doc collection 1 "timeCreated" : ISODate("2015-11-19T02:09:51.413+0000"), }, { "_id" : ObjectId("564d2f6fb0896138247ff855"), "latitude" : 48.5783611, "longitude" : -72.9669167, "parent_id" : ObjectId("564d2f6eb0896138247ff791"), <-- matches doc collection 1 "timeCreated" : ISODate("2015-11-19T02:09:51.413+0000"), } ] } 

我曾经想过将这些Schema集成到一个集合中,但是我不能因为我将添加到代码中的其他function,比如存储图片和每个孩子的编辑。 我不认为使用单个集合是明智的,因为我将在文档中存储太多的信息。

谢谢你的帮助!

我认为你可以使用一对多的文档引用模型 。 也就是说,将位置参考(仅_id)存储在洞穴文件中。

 { "_id" : ObjectId("564d2f6eb0896138247ff791"), "name" : "ACME Cave", "slug" : "acme-cave", "timeCreated" : ISODate("2015-11-19T02:09:50.492+0000"), "address" : { "county" : "Somewhere", "state" : "NY", "country" : "USA" }, locations: [ 564d2f6fb0896138247ff855, 564d2f6fb0896138247ff855 ] }