场景:评论区
表:一级评论表comments、二级评论表reply、用户表user
问题:现在是根据course来查询对应的一级评论和该一级评论下的二级评论,然后两个表中都有from (来自用户)字段。想根据from字段去联查出该用户的头像、昵称。不知道该怎么实现了....
我目前的代码只能获得这样的结果:
return db
    .collection("comments")
    .aggregate()
    .match({
      course: event.course,
    })
    .lookup({
      from: "user",
      localField: "from",
      foreignField: "_id",
      as: "userInfo",
    })
    .lookup({
      from: 'replies',
      localField: '_id',
      foreignField: 'reply',
      as: 'children',
    })
    .replaceRoot({
      newRoot: $.mergeObjects([$.arrayElemAt(["$userInfo", 0]), "$$ROOT"]),
    })
    .project({
      userInfo: 0,
      city: 0,
      college: 0,
      country: 0,
      gender: 0 ,
      introduction: 0,
      language: 0,
      praised: 0,
      province: 0
    })
    .end({
      success: function (res) {
        return res;
      },
      fail(error) {
        return error;
      },
    });

合表吧;lookup超过两级联表的,就表示该合表了。
course表:
{ //其他course字段 comments: [ { comment: '这是评论', avartarUrl, nickName, openid, time, replies: [ { reply: '这是回复', avatarUrl, nickName, openid, time } ] } ] }avatarUrl保存的头像链接,nickName保存昵称,都已经赋值了,还要去哪拿?