1
感谢您的阅读。我试图获取我存储在mongodb数据库中的大量数据,以便在NodeJS中使用mongoose模块显示在前端。我想知道解决这个问题的最好方法是什么?我的数据是这样的......MongoDB/Node:将集合中的文档的一部分插入另一个集合中的文档
收藏1:洞穴
{
"_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"
}
}
系列2:位置
{
"_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);
});
});
这将返回follwing 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"),
}
]
}
我曾想过这些模式卷起成1个集,但我不能因为其他功能我将会添加到代码中,比如存储图片和每个孩子的编辑。我不认为使用单个集合是明智的,因为我将在文档中存储太多信息。
感谢您的帮助!
这不是一个坏主意!我一直在思考这个问题,但我一直在想,如果我有一个随机的位置文件,并且我需要获得父项?我想可以保存parent_id。我认为这是一个不好的方法,或者在每个集合中都存储这两种情况很常见?我来自强大的关系数据库背景,让我的头脑变得粗糙。 – devinception
在MongoDB中,针对您的问题有不同的解决方案。我推荐以下系列文章:[6 MongoDB Schema Design的经验法则](http://blog.mongodb.org/post/87200945828/6-rules-of-thumb-for-mongodb-schema-design-part -1)。 – hecnabae
感谢您的帮助,我决定遵循您的示例并将ObjectIds存储在洞穴文档中。 – devinception