聚合返回一个空数组

发布于 2025-02-09 07:30:04 字数 3875 浏览 2 评论 0原文

我一直在使用 mongoose odm nodejs 环境,在我的开发过程中,我遇到了这样的问题。我需要获取文档列表,并对这些文档执行一些汇总操作。为了获取特定文档,我使用了 $匹配聚集,但它行不通, 我试图在没有$匹配阶段的情况下使用,所以它起作用(它返回了一些文档), 但是我必须指定确切的字段,并且使用了匹配项,但是它返回一个空数组。 我的管道下面(使用猫鼬):

const comments = await Comments.aggregate([
            {
                '$match': {
                    'article_id': Types.ObjectId(id)
                }
            },
            {
                '$unwind':
                    {
                        path: '$replies'
                    }
            },
            {
                '$lookup': {
                    from: 'users',
                    localField: 'replies.sender',
                    foreignField: '_id',
                    'pipeline': [
                        {'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
                    ],
                    as: 'replies.sender'
                }
            },
            {
                '$unwind': {
                    path: '$replies.sender'
                }
            },
            {
                '$group': {
                    '_id': '$_id',
                    'replies': {
                        '$push': '$replies'
                    }
                }
            },
            {
                '$lookup': {
                    from: 'comments',
                    localField: '_id',
                    foreignField: '_id',
                    as: 'commentDetails'
                }
            },
            {
                '$unwind': {
                    path: '$commentDetails'
                }
            },
            {
                '$addFields': {
                    'commentDetails.replies': '$replies'
                }
            },
            {
                '$replaceRoot': {
                    'newRoot': '$commentDetails'
                }
            },
            {
                '$unwind':
                    {
                        path: '$replies'
                    }
            },
            {
                '$lookup': {
                    from: 'users',
                    localField: 'replies.receiver',
                    foreignField: '_id',
                    'pipeline': [
                        {'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
                    ],
                    as: 'replies.receiver'
                }
            },
            {
                '$unwind': {
                    path: '$replies.receiver'
                }
            },
            {
                '$group': {
                    '_id': '$_id',
                    'replies': {
                        '$push': '$replies'
                    }
                }
            },
            {
                '$lookup': {
                    from: 'comments',
                    localField: '_id',
                    foreignField: '_id',
                    as: 'commentDetails'
                }
            },
            {
                '$unwind': {
                    path: '$commentDetails'
                }
            },
            {
                '$addFields': {
                    'commentDetails.replies': '$replies'
                }
            },
            {
                '$replaceRoot': {
                    'newRoot': '$commentDetails'
                }
            },
            {
                '$lookup': {
                    'from': 'users',
                    'localField': 'user_id',
                    'foreignField': '_id',
                    'pipeline': [
                        {'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
                    ],
                    'as': 'user'
                }
            },
            {
                '$unwind': '$user'
            },
        ]).sort({'date_time': 'descending'})

I have been using mongoose ODM with Nodejs environment, during my development I faced such problem. I need to get a list of docs and perform some aggregation operations on these docs. In order to get specific docs I used $match aggregation, but it does not work,
I have tried to use without $match stage, so it worked (it returned some docs),
but I have to specify exact field and I used match, but it returns an empty array.
My pipeline below (using mongoose):

const comments = await Comments.aggregate([
            {
                '$match': {
                    'article_id': Types.ObjectId(id)
                }
            },
            {
                '$unwind':
                    {
                        path: '$replies'
                    }
            },
            {
                '$lookup': {
                    from: 'users',
                    localField: 'replies.sender',
                    foreignField: '_id',
                    'pipeline': [
                        {'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
                    ],
                    as: 'replies.sender'
                }
            },
            {
                '$unwind': {
                    path: '$replies.sender'
                }
            },
            {
                '$group': {
                    '_id': '$_id',
                    'replies': {
                        '$push': '$replies'
                    }
                }
            },
            {
                '$lookup': {
                    from: 'comments',
                    localField: '_id',
                    foreignField: '_id',
                    as: 'commentDetails'
                }
            },
            {
                '$unwind': {
                    path: '$commentDetails'
                }
            },
            {
                '$addFields': {
                    'commentDetails.replies': '$replies'
                }
            },
            {
                '$replaceRoot': {
                    'newRoot': '$commentDetails'
                }
            },
            {
                '$unwind':
                    {
                        path: '$replies'
                    }
            },
            {
                '$lookup': {
                    from: 'users',
                    localField: 'replies.receiver',
                    foreignField: '_id',
                    'pipeline': [
                        {'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
                    ],
                    as: 'replies.receiver'
                }
            },
            {
                '$unwind': {
                    path: '$replies.receiver'
                }
            },
            {
                '$group': {
                    '_id': '$_id',
                    'replies': {
                        '$push': '$replies'
                    }
                }
            },
            {
                '$lookup': {
                    from: 'comments',
                    localField: '_id',
                    foreignField: '_id',
                    as: 'commentDetails'
                }
            },
            {
                '$unwind': {
                    path: '$commentDetails'
                }
            },
            {
                '$addFields': {
                    'commentDetails.replies': '$replies'
                }
            },
            {
                '$replaceRoot': {
                    'newRoot': '$commentDetails'
                }
            },
            {
                '$lookup': {
                    'from': 'users',
                    'localField': 'user_id',
                    'foreignField': '_id',
                    'pipeline': [
                        {'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
                    ],
                    'as': 'user'
                }
            },
            {
                '$unwind': '$user'
            },
        ]).sort({'date_time': 'descending'})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2025-02-16 07:30:04

我找到了一个解决方案。我刚刚添加了preservenullandemptyarrays:true <代码> $ undind

I found a solution. I just added preserveNullAndEmptyArrays: true to $unwind

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文