根据子文档从文档列表中创建输出为父子

时间:2016-04-21 06:12:47

标签: node.js mongodb mongoose subdocument

我在数据库中记录如下(http://www.jsoneditoronline.org/?id=cb05d8495014d3581acaf3fa466d5e4c

          [{
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "_id" : ObjectId("XXX"),
            "controlprops" : {
                "height" : "200px",
                "width" : "300px",
                "position" : "absolute",
                "X" : "387px",
                "Y" : "228px",
                "sequenceId" : 1,
                "ParentId" : null,
                "Id" : "hKNBdf0"
            }
        }, 
        {
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "_id" : ObjectId("XXX"),
            "controlprops" : {
                "height" : "110px",
                "width" : "244px",
                "position" : "absolute",
                "X" : "16px",
                "Y" : "40px",
                "sequenceId" : 1,
                "ParentId" : "hKNBdf0",
                "Id" : "8aqaU39"
            }
        }, 
        {
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "_id" : ObjectId("XXX"),
            "controlprops" : {
                "height" : "110px",
                "width" : "244px",
                "position" : "absolute",
                "X" : "16px",
                "Y" : "40px",
                "sequenceId" : 1,
                "ParentId" : "hKNBdf0",
                "Id" : "8aqaU39"
            }
        }, 
        {
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "_id" : ObjectId("571868926383b14c1a1fbb1c"),
            "controlprops" : {
                "height" : "38px",
                "width" : "100px",
                "position" : "absolute",
                "X" : "48px",
                "Y" : "37px",
                "background_color" : "green",
                "color" : "rgb(255, 255, 255)",
                "sequenceId" : 2,
                "ParentId" : null,
                "Id" : "eSJfjXp"
            }
        }]

这里基于" ParentId"," Id" in" controlprops"我想要与父子关系的对象。

此输出将类似于(http://www.jsoneditoronline.org/?id=cb05d8495014d3581acaf3fa4617ea52):

 [{
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "controlprops" : {
                "height" : "200px",
                "width" : "300px",
                "position" : "absolute",
                "X" : "387px",
                "Y" : "228px",
                "sequenceId" : 1,
                "ParentId" : null,
                "Id" : "hKNBdf0"
            },
            "child" : [
              {
             "X" : "ValueofX",
             "Y" : "ValueofY",
            "controlprops" : {
                "height" : "110px",
                "width" : "244px",
                "position" : "absolute",
                "X" : "16px",
                "Y" : "40px",
                "sequenceId" : 1,
                "ParentId" : "hKNBdf0",
                "Id" : "8aqaU39"
             }
            },{
             "X" : "ValueofX",
             "Y" : "ValueofY",
            "controlprops" : {
                "height" : "110px",
                "width" : "244px",
                "position" : "absolute",
                "X" : "16px",
                "Y" : "40px",
                "sequenceId" : 1,
                "ParentId" : "hKNBdf0",
                "Id" : "8aqaU39"
             }
            }
            ]
        }
        , 
        {
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "controlprops" : {
                "height" : "38px",
                "width" : "100px",
                "position" : "absolute",
                "X" : "48px",
                "Y" : "37px",
                "background_color" : "green",
                "color" : "rgb(255, 255, 255)",
                "sequenceId" : 2,
                "ParentId" : null,
                "Id" : "eSJfjXp"
            }
        }]

我如何使用mongodb / mongoose实现它? 任何帮助将不胜感激。

0 个答案:

没有答案