Json 在Mongodb中使用聚合、查找和管道进行查询

Json 在Mongodb中使用聚合、查找和管道进行查询,json,mongodb,mongodb-query,aggregation-framework,lookup,Json,Mongodb,Mongodb Query,Aggregation Framework,Lookup,我必须管理一个有注释的墙,每个墙都有许多父注释,每个父注释都有子注释 我的收藏是这样的 groupId : {type: Schema.Types.ObjectId, ref: 'groups', unique: true}, comments : [{ commentId : {type: Schema.Types.ObjectId, ref: 'comments'}, user : {type: Schema.Types.ObjectId, ref:

我必须管理一个有注释的墙,每个墙都有许多父注释,每个父注释都有子注释

我的收藏是这样的

 groupId : {type: Schema.Types.ObjectId, ref: 'groups', unique: true},
    comments : [{
        commentId : {type: Schema.Types.ObjectId, ref: 'comments'},
        user : {type: Schema.Types.ObjectId, ref: 'users'},
    }],
收集的评论是这样的

text : String,
parentCommentId : {type: Schema.Types.ObjectId, ref: 'comments', default : null},
我想按父注释显示墙,每个子注释位于其父注释下

我尝试了这个查询,但没有返回任何结果

db.getCollection('walls').aggregate([
        {$match: {groupId: ObjectId("5e8c5caa75b1cd342a1175eb")}},
        {
            "$lookup": {
                from: "comments",
                let: { item: "$comments.commentId" },
                pipeline: [
                    { $match:
                        { $expr: { $eq: [ "$parentCommentId",  "$$item" ] }


                        }
                    },
                    { $project: {
                        "_id": 1,
                        "parentCommentId": 1,
                        "text": 1                       
                    } }
                ],
                as: "comments"
            }
        },
        {
            $project: {
                groupId: 1,
                "comments":1,
                date: 1
            }
        }
    ])
墙中的数据

{
    "_id" : ObjectId("5e95b4b49d3e303d667a8b71"),
    "groupId" : ObjectId("5e8c5caa75b1cd342a1175eb"),
    "comments" : [ 
        {
            "_id" : ObjectId("5e95b4b49d3e303d667a8b72"),
            "commentId" : ObjectId("5e95b4b49d3e303d667a8b70")
        }, 
        {
            "_id" : ObjectId("5e95b4ef80ae1244693aa857"),
            "commentId" : ObjectId("5e95b4ef80ae1244693aa856")
        }, 
        {
            "_id" : ObjectId("5e95b51080ae1244693aa859"),
            "commentId" : ObjectId("5e95b51080ae1244693aa858")
        }, 
        {
            "_id" : ObjectId("5e95b51d80ae1244693aa85b"),
            "commentId" : ObjectId("5e95b51d80ae1244693aa85a")
        },
        {
           "_id" : ObjectId("5e95b53580ae1244693aa85e"),
           "commentId" : ObjectId("5e95b53580ae1244693aa85c")
        }
    ],

}
{
    "_id" : ObjectId("5e95b4b49d3e303d667a8b70"),
    "parentCommentId" : null,
    "text" : "Hello parent 1"
}

{
    "_id" : ObjectId("5e95b4ef80ae1244693aa856"),
    "parentCommentId" : null,
    "text" : "Hello parent 2",
    "date" : ISODate("2020-04-14T13:04:47.860Z")
}

{
    "_id" : ObjectId("5e95b51080ae1244693aa858"),
    "parentCommentId" : ObjectId("5e95b4b49d3e303d667a8b70"),
    "text" : "Hello child 1 parent 1"
}

{
    "_id" : ObjectId("5e95b51d80ae1244693aa85a"),
    "parentCommentId" : ObjectId("5e95b4b49d3e303d667a8b70"),
    "text" : "Hello child 2 parent 1"

}

{
    "_id": "5e95b53580ae1244693aa85c",
    "parentCommentId": "5e95b4ef80ae1244693aa856",
    "text": "Hello child 1 parent 2",
}
评论中的数据

{
    "_id" : ObjectId("5e95b4b49d3e303d667a8b71"),
    "groupId" : ObjectId("5e8c5caa75b1cd342a1175eb"),
    "comments" : [ 
        {
            "_id" : ObjectId("5e95b4b49d3e303d667a8b72"),
            "commentId" : ObjectId("5e95b4b49d3e303d667a8b70")
        }, 
        {
            "_id" : ObjectId("5e95b4ef80ae1244693aa857"),
            "commentId" : ObjectId("5e95b4ef80ae1244693aa856")
        }, 
        {
            "_id" : ObjectId("5e95b51080ae1244693aa859"),
            "commentId" : ObjectId("5e95b51080ae1244693aa858")
        }, 
        {
            "_id" : ObjectId("5e95b51d80ae1244693aa85b"),
            "commentId" : ObjectId("5e95b51d80ae1244693aa85a")
        },
        {
           "_id" : ObjectId("5e95b53580ae1244693aa85e"),
           "commentId" : ObjectId("5e95b53580ae1244693aa85c")
        }
    ],

}
{
    "_id" : ObjectId("5e95b4b49d3e303d667a8b70"),
    "parentCommentId" : null,
    "text" : "Hello parent 1"
}

{
    "_id" : ObjectId("5e95b4ef80ae1244693aa856"),
    "parentCommentId" : null,
    "text" : "Hello parent 2",
    "date" : ISODate("2020-04-14T13:04:47.860Z")
}

{
    "_id" : ObjectId("5e95b51080ae1244693aa858"),
    "parentCommentId" : ObjectId("5e95b4b49d3e303d667a8b70"),
    "text" : "Hello child 1 parent 1"
}

{
    "_id" : ObjectId("5e95b51d80ae1244693aa85a"),
    "parentCommentId" : ObjectId("5e95b4b49d3e303d667a8b70"),
    "text" : "Hello child 2 parent 1"

}

{
    "_id": "5e95b53580ae1244693aa85c",
    "parentCommentId": "5e95b4ef80ae1244693aa856",
    "text": "Hello child 1 parent 2",
}
期望的结果

{
    "success": true,
    "data": [
        {
            "_id": "5e95b4b49d3e303d667a8b71",
            "groupId": "5e8c5caa75b1cd342a1175eb",
            "comments": [
                {
                    "_id": "5e95b4b49d3e303d667a8b70",
                    "parentCommentId": null,
                    "text": "Hello parent 1",
                    "childs": {
                                 {
                                   "_id": "5e95b51080ae1244693aa858",
                                   "parentCommentId": "5e95b4b49d3e303d667a8b70",
                                   "text": "Hello child 1 parent 1",
                                 },
                                 {
                                   "_id": "5e95b51d80ae1244693aa85a",
                                   "parentCommentId": "5e95b4b49d3e303d667a8b70",
                                   "text": "Hello child 2 parent 1",
                                 },
                              }
                },
                {
                    "_id": "5e95b4ef80ae1244693aa856",
                    "parentCommentId": null,
                    "text": "Hello parent 2",
                    "childs": {
                                  {
                                    "_id": "5e95b53580ae1244693aa85c",
                                    "parentCommentId": "5e95b4ef80ae1244693aa856",
                                    "text": "Hello child 1 parent 2",
                                 }
                         }
                },


            ],
         }
    ]
}

如何修改我的查询?谢谢。

您可以使用下面的聚合

db.walls.aggregate([
  { "$lookup": {
    "from": "comments",
    "let": { "commentIds": "$comments.commentId" },
    "pipeline": [
      { "$match": {
        "$expr": { "$in": ["$_id", "$$commentIds"] },
        "parentCommentId": null
      }},
      { "$sort": { "text": -1 }},
      { "$graphLookup": {
        "from": "comments",
        "startWith": "$_id",
        "connectFromField": "parentCommentId",
        "connectToField": "parentCommentId",
        "as": "childs"
      }}
    ],
    "as": "comments"
  }}
])

您可以使用下面的聚合

db.walls.aggregate([
  { "$lookup": {
    "from": "comments",
    "let": { "commentIds": "$comments.commentId" },
    "pipeline": [
      { "$match": {
        "$expr": { "$in": ["$_id", "$$commentIds"] },
        "parentCommentId": null
      }},
      { "$sort": { "text": -1 }},
      { "$graphLookup": {
        "from": "comments",
        "startWith": "$_id",
        "connectFromField": "parentCommentId",
        "connectToField": "parentCommentId",
        "as": "childs"
      }}
    ],
    "as": "comments"
  }}
])