将嵌套文档集合转换为具有父引用的模型树结构

时间:2016-01-10 21:41:44

标签: javascript mongodb meteor minimongo

我需要将带有嵌套文档的集合转换为带有父引用的模型树结构。这就是我的结构的样子:

{
    "_id" : "sdaGREsfRdfGdFdwG",
    "docTitle" : "Document 1",
    "group" : [
        {
            "id" : "cdPhkTpMXi8z6TqMT"
            "title" : "title 1",
            "data" : [
                {
                    "id" : "Nkspf5kKfPo3axeJA",
                    "some" : "data",
                    "other" : "things",
                    "and" : "so on",    
                },
                {
                    "id" : "Ca8ncFgq83RoeD8he",
                    "some" : "data",
                    "other" : "things",
                    "and" : "so on",    
                },
                {
                    "id" : "vyjgkuNXRN9KkCd5o",
                    "some" : "data",
                    "other" : "things",
                    "and" : "so on",    
                }
            ],

        },
        {
            "id" : "TuibXPe5qMvqdMW6q"
            "title" : "title 2",
            "data" : [
                {
                    "id" : "f5L5zsSNRSQKWoAXL",
                    "some" : "data",
                    "other" : "things",
                    "and" : "so on",    
                }
            ],

        }
    ]
}

如您所见,有一个组数组,它​​也有一个数据数组。现在我需要一个这样的主文档:

{
    "_id" : "sdaGREsfRdfGdFdwG",
    "docTitle" : "Document 1"
}

下一个子元素,即:

{
    "id" : "cdPhkTpMXi8z6TqMT"
    "title" : "title 1",
    "type" : "group",
    "parent" : "sdaGREsfRdfGdFdwG"
},
{
    "id" : "TuibXPe5qMvqdMW6q"
    "title" : "title 2",
    "type" : "group",
    "parent" : "sdaGREsfRdfGdFdwG"
}

至少是数据元素,它们是group-elements的子元素:

{
    "id" : "Nkspf5kKfPo3axeJA",
    "some" : "data",
    "other" : "things",
    "and" : "so on",
    "type" : "element",
    "parent" : "cdPhkTpMXi8z6TqMT"
},
{
    "id" : "Ca8ncFgq83RoeD8he",
    "some" : "data",
    "other" : "things",
    "and" : "so on",
    "type" : "element",
    "parent" : "cdPhkTpMXi8z6TqMT"  
},
{
    "id" : "vyjgkuNXRN9KkCd5o",
    "some" : "data",
    "other" : "things",
    "and" : "so on",
    "type" : "element",
    "parent" : "cdPhkTpMXi8z6TqMT"  
},
{
    "id" : "f5L5zsSNRSQKWoAXL",
    "some" : "data",
    "other" : "things",
    "and" : "so on",
    "type" : "element",
    "parent" : "TuibXPe5qMvqdMW6q"      
}

所以我的尝试将是这样的:

Collection.find({}).forEach(function(doc) {     // get all documents
    var docID = doc._id;

    doc.group.forEach(function(group) {         // get all groups
        var parent = group.id;
        group.type = "group";
        group.parent = docID;

        group.data.forEach(function(element) {  // get all elements
            element.type ="element";
            element.parent = parent;
        });
    });
});

1)我不知道如何以更好的方式做到这一点

2)我不知道如何将结果保存回集合

1 个答案:

答案 0 :(得分:1)

Fot the groups:

db.myCol.aggregate([{$unwind:"$group"}, 
    {$project:{_id:"$group.id", some:"$group.some", 
               parent:"$_id", type:{$literal:"group"}}}])

对于元素:

db.myCol.aggregate([{$unwind:"$group"}, 
                    {$unwind:"$group.data"}, 
                    {$project:{_id:"$group.data.id", some:"$group.data.some", 
                               parent:"$group.id", type:{$literal:"element"}}}])

关于第二个问题:您可以在最后向汇总管道添加$ out步骤。例如:{$ out:“outputCol”}

相关问题