0

我有一个这样的聚合:

this.collection.aggregate([
      {
        "$match": {
          _id: id
        }
      },
      {
        "$addFields": {
          "self": "$$ROOT"
        }
      },
      {
        "$graphLookup": {
          "from": "posts",
          "startWith": "$_id",
          "connectFromField": "_id",
          "connectToField": "postId",
          "as": "postLookup"
        }
      },
      {
        "$addFields": {
          "postLookup": {
            "$concatArrays": [
              "$postLookup",
              [
                "$self"
              ]
            ]
          }
        }
      },
      {
        "$lookup": {
          "from": "comments",
          "localField": "postLookup._id",
          "foreignField": "postId",
          "as": "commentsLookup"
        }
      },
      {
        "$project": {
          children: {
            posts: {
              "$map": {
                "input": "$postLookup",
                "as": "c",
                "in": "$$c._id"
              }
            },
            comments: {
              "$map": {
                "input": "$commentsLookup",
                "as": "t",
                "in": "$$t._id"
              }
            }
          }
        }
      }
    ]);

所以预期的结果应该是这样的:

{
   children: {
        posts: {
            Array
          },
        comments: {
            Array
          }
}
}

问题是聚合函数返回一个不能转换为数组的游标,因为它不是文档列表,它是一个对象,您可以在其中找到数组。我无法从光标本身获得这两个数组。有人可以指导我了解我在这里缺少的内容。谢谢

4

0 回答 0