mongoose不要选择与任何相关的记录

发布于 2025-02-11 06:42:35 字数 890 浏览 0 评论 0原文

我能够成功实施查找,但问题在于;我不需要没有任何记录的记录可以加入儿童收集。目前,如果没有在儿童收藏中找到的匹配记录,我将获得空白的阵列。注意:我正在使用MongoDB灵活模式

parentCollection.aggregate([ 
    { 
        '$match': { 
            userId: '62810c69e880ed667073add1',
            type: 'data' 
        } 
    }, 
    { 
        '$lookup': { 
            from: 'childCollection', 
            let: { typeId: { '$toObjectId': '$typeId' } }, 
            pipeline: [ { 
                '$match': { 
                    '$expr': { '$eq': [ '$_id', '$$typeId' ] }, 
                    '$or': [ { 
                        'field1': { '$regex': '737', '$options': 'i' } 
                    }, 
                    { 
                        'field2': { '$regex': '737', '$options': 'i' } 
                    }] 
                }
            }], 
            as: 'child' 
        } 
    }, 
    { '$limit': 10 }, { '$skip': 0 }], 
{});

I am able to successfully implement the lookup, but the problem is that; I don't need the records which doesn't have any record to join in child collection. Currently I am getting blank array for child field, if no matching record found in child collection. Note: I am using MongoDB Flexible Schema

parentCollection.aggregate([ 
    { 
        '$match': { 
            userId: '62810c69e880ed667073add1',
            type: 'data' 
        } 
    }, 
    { 
        '$lookup': { 
            from: 'childCollection', 
            let: { typeId: { '$toObjectId': '$typeId' } }, 
            pipeline: [ { 
                '$match': { 
                    '$expr': { '$eq': [ '$_id', '$typeId' ] }, 
                    '$or': [ { 
                        'field1': { '$regex': '737', '$options': 'i' } 
                    }, 
                    { 
                        'field2': { '$regex': '737', '$options': 'i' } 
                    }] 
                }
            }], 
            as: 'child' 
        } 
    }, 
    { '$limit': 10 }, { '$skip': 0 }], 
{});

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

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

发布评论

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

评论(1

鹿! 2025-02-18 06:42:35

我已经通过在$查找后添加另一个$匹配来实现这一目标

parentCollection.aggregate([ 
        { 
            '$match': { 
                userId: '62810c69e880ed667073add1',
                type: 'data' 
            } 
        }, 
        { 
            '$lookup': { 
                from: 'childCollection', 
                let: { typeId: { '$toObjectId': '$typeId' } }, 
                pipeline: [ { 
                    '$match': { 
                        '$expr': { '$eq': [ '$_id', '$typeId' ] }, 
                        '$or': [ { 
                            'field1': { '$regex': '737', '$options': 'i' } 
                        }, 
                        { 
                            'field2': { '$regex': '737', '$options': 'i' } 
                        }] 
                    }
                }], 
                as: 'child' 
            } 
        },{
            $match: {
                $expr: {
                    $gt: [{
                        $size: '$child'
                    }, 0]
                }
            }
        },
        { '$limit': 10 }, { '$skip': 0 }], 
{});

I have achieved this by adding an additional $match on child after $lookup

parentCollection.aggregate([ 
        { 
            '$match': { 
                userId: '62810c69e880ed667073add1',
                type: 'data' 
            } 
        }, 
        { 
            '$lookup': { 
                from: 'childCollection', 
                let: { typeId: { '$toObjectId': '$typeId' } }, 
                pipeline: [ { 
                    '$match': { 
                        '$expr': { '$eq': [ '$_id', '$typeId' ] }, 
                        '$or': [ { 
                            'field1': { '$regex': '737', '$options': 'i' } 
                        }, 
                        { 
                            'field2': { '$regex': '737', '$options': 'i' } 
                        }] 
                    }
                }], 
                as: 'child' 
            } 
        },{
            $match: {
                $expr: {
                    $gt: [{
                        $size: '$child'
                    }, 0]
                }
            }
        },
        { '$limit': 10 }, { '$skip': 0 }], 
{});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文