如何基于Id合并mongodb中的文档

时间:2018-03-14 08:57:03

标签: mongodb mongoose mongodb-query aggregation-framework

mongoDb中是否有办法根据id合并文档。例如:

/*1*/{
"id" : "xxxxxx",
"pages" : {
    "name" : "Page50-50",
    "pageid" : "Page50-50",
    "icon" : "/images/cmstemplates/2column.png",
    "children" : []
 },
 "permissions" : [ 
    {
        "grpName" : "Admin",
        "grants" : [ 
            "View", 
            "Delete", 
            "Edit"
        ]
    }, 
    {
        "grpName" : "Users",
        "grants" : [ 
            "View"
        ]
    }
]
}
/*2*/
{
"id" : "xxxxxx",
"pages" : {
    "name" : "AboutUs",
    "pageid" : "AboutUs",
    "icon" : "/images/cmstemplates/1column.png",
    "children" : []
 },
 "permissions" : [ 
    {
        "grpName" : "Student",
        "grants" : [ 
            "View", 
            "Delete"
        ]
    }
]
}
Expected Output:
{
"id" : "xxxxxx",
"pages" : [{
    "name" : "Page50-50",
    "pageid" : "Page50-50",
    "icon" : "/images/2column.png",
    "children" : [],
    "permissions" : [ 
    {
        "grpName" : "Admin",
        "grants" : [ 
            "View", 
            "Delete", 
            "Edit"
        ]
    }, 
    {
        "grpName" : "Users",
        "grants" : [ 
            "View"
        ]
    }
]
},{
    "name" : "AboutUs",
    "pageid" : "AboutUs",
    "icon" : "/images/1column.png",
    "children" : [],
    "permissions" : [ 
    {
        "grpName" : "Student",
        "grants" : [ 
            "View", 
            "Delete"
        ]
    }
]
}]
}

预期输出中的权限参数应该在页面内部,页面应该分组到一个数组中。我使用$ group并能够对页面进行分组,但我无法弄清楚如何在页面内添加权限参数。我正在使用mongodb 3.2。

感谢。

1 个答案:

答案 0 :(得分:1)

使用$ group

img{
  width: 100%;
}
.img1{
  width: calc(100% / 8);
  float:left;
}

@media (max-width: 860px) {
  .img1{
  width: calc(100% / 4);
  float:left;
  }

}