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

Convert nested document collection to model tree structure with parent references

本文关键字:引用 模型树 结构 文档 嵌套 集合 转换      更新时间:2023-09-26

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

{
    "_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"
}

至少是数据元素,它们是组元素的子元素:

{
    "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) 我不知道如何将结果保存回集合

分组:

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"}