如何填充$ lookup的结果

问题描述

我有3个收藏集,帖子,评论和用户,帖子和评论的模式如下:

发布架构

const PostSchema: Schema = new Schema({
    author: { type: String,ref: 'User' },postID: {type: String},text: { type: String },});

评论架构

const commentSchema: Schema = new Schema({
    author: { type: Schema.Types.ObjectId,parentPost: {type: Schema.Types.ObjectId,ref: 'Post'},parentPostID: {type: String},// Post that was commented on
});

要获取每个帖子的评论,请执行汇总查询:

 const aggregateQuery = [
                            {
                                $match:{postID:{ $in: postKeys},},{
                                $lookup:
                                    {
                                        from: 'comments',localField: '_id',foreignField: 'parentPost',as: 'Top_Comment',{
                                $sort: {
                                    createdAt: 1,]

该查询有效,但是我无法弄清楚如何填充评论的author属性,因为它是对用户的引用。

我尝试过的

我尝试使用管道,但是我是聚合的新手,所以我不知道如何,任何想法和解决方案都将受到赞赏

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...