父子层次结构mongodb聚合管道

时间:2019-04-22 13:01:53

标签: mongodb aggregation-framework aggregate parent-child aggregation

我的收藏中的文件

#sidebarCollapse {
    display: none;
  }

我当前的聚合管道

/* 1 createdAt:4/22/2019, 4:53:32 PM*/
{
    "_id" : ObjectId("5cbdab3c9bd57541040342a1"),
    "created_on" : "2017-03-07T21:07:04.778Z",
    "feature" : "Source",
    "feature_option" : "write",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : "2017-03-07T21:07:04.778Z",
    "name" : "Read",
    "parent_feature" : "Contact"
},

/* 2 createdAt:4/22/2019, 4:52:58 PM*/
{
    "_id" : ObjectId("5cbdab1a9bd575410403429f"),
    "created_on" : "2017-03-07T21:07:04.778Z",
    "feature" : "Source",
    "feature_option" : "read",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : "2017-03-07T21:07:04.778Z",
    "name" : "Read",
    "parent_feature" : "Contact"
},

/* 3 createdAt:5/9/2018, 8:41:42 PM*/
{
    "_id" : ObjectId("5af316b6141292374cbf363b"),
    "created_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "feature" : "Survey",
    "feature_option" : "delete",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "name" : "Delete",
    "feature_id" : "1",
    "parent_feature" : null
},

/* 4 createdAt:5/9/2018, 8:41:42 PM*/
{
    "_id" : ObjectId("5af316b6141292374cbf363a"),
    "created_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "feature" : "Survey",
    "feature_option" : "edit",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "name" : "Edit",
    "feature_id" : "1",
    "parent_feature" : null
},

/* 5 createdAt:5/9/2018, 8:41:42 PM*/
{
    "_id" : ObjectId("5af316b6141292374cbf3639"),
    "created_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "feature" : "Survey",
    "feature_option" : "write",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "name" : "Write",
    "parent_feature" : null
},

/* 6 createdAt:5/9/2018, 8:41:42 PM*/
{
    "_id" : ObjectId("5af316b6141292374cbf3638"),
    "created_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "feature" : "Survey",
    "feature_option" : "read",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "name" : "Read",
    "parent_feature" : null
},

/* 7 createdAt:5/9/2018, 8:41:27 PM*/
{
    "_id" : ObjectId("5af316a7141292374cbf3637"),
    "created_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "feature" : "Contact",
    "feature_option" : "delete",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "name" : "Delete",
    "parent_feature" : "Survey"
},

/* 8 createdAt:5/9/2018, 8:41:27 PM*/
{
    "_id" : ObjectId("5af316a7141292374cbf3636"),
    "created_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "feature" : "Contact",
    "feature_option" : "edit",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "name" : "Edit",
    "parent_feature" : "Survey"
},

/* 9 createdAt:5/9/2018, 8:41:27 PM*/
{
    "_id" : ObjectId("5af316a7141292374cbf3635"),
    "created_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "feature" : "Contact",
    "feature_option" : "write",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "name" : "Write",
    "parent_feature" : "Survey"
},

/* 10 createdAt:5/9/2018, 8:41:27 PM*/
{
    "_id" : ObjectId("5af316a7141292374cbf3634"),
    "created_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "feature" : "Contact",
    "feature_option" : "read",
    "icon" : "fa fa-plus",
    "is_deleted" : false,
    "level" : 2,
    "modified_on" : ISODate("2017-03-08T02:07:04.778+05:00"),
    "name" : "Read",
    "parent_feature" : "Survey"
}

我当前的输出:

db.feature2.aggregate([
        {
            $group: {
                _id: {
                    parent: "$parent_feature",
                    feature: "$feature"
                },
                features: { $push: "$feature_option" }
            }
        },
        {
            $group: {
                _id: "$_id.parent",
                features: { $push: { name: "$_id.feature", feature: "$features" } }
            }
        },
        {
            $project: {
                _id: 0,
                parent: "$_id",
                features: 1
            }
        }
    ]);

我想要的输出:

   /* 1 */
{
    "features" : [
        {
            "name" : "Survey",
            "feature" : [
                "read",
                "write",
                "edit",
                "delete"
            ]
        }
    ],
    "parent" : null
},

/* 2 */
{
    "features" : [
        {
            "name" : "Source",
            "feature" : [
                "read",
                "write"
            ]
        }
    ],
    "parent" : "Contact"
},

/* 3 */
{
    "features" : [
        {
            "name" : "Contact",
            "feature" : [
                "read",
                "write",
                "edit",
                "delete"
            ]
        }
    ],
    "parent" : "Survey"
}

我已经为此工作了一段时间,我尝试了各种尝试,但未能成功。我的数据中有父子关系。我想要的是,主要父项(其父项为null表示没有任何父项)位于顶部,然后我希望每个父项内的子项数组。提前致谢!

0 个答案:

没有答案