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

时间:2015-11-20 06:53:33

标签: json node.js mongodb mongoose

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

收集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);
    });
});

这将返回以下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汇总到一个集合中,但我不能因为其他功能我将添加到代码中,比如存储图片和每个孩子的编辑。我不认为使用单个集合是明智的,因为我会在文档中存储太多信息。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为您可以使用one-to-many with document references model。也就是说,将位置引用(仅_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 ]
}