【问题标题】:search query mongodb using aggregation with $lookup and $match使用 $lookup 和 $match 聚合搜索查询 mongodb
【发布时间】:2020-03-28 20:10:20
【问题描述】:

我正在为一个我不知道如何真实的查询而苦苦挣扎......我假装做一个搜索输入,在那里我想找到一个客户并从这个客户那里获得我需要的所有信息。 我有 3 个收藏。

客户集合

{
    _id: '5e3376249110c43528f31101'
    name: 'Jhon',
    rate: '5e790595b0d727313cb56d3b',
}

费率收集

{
    _id: '5e790595b0d727313cb56d3b'
    title: 'Rate for sellers'
    margin: 10,    
}

配置集合

{
    _id: '5e7276249110c43528f21402'
    idRate: '5e790595b0d727313cb56d3b'
    maring: 8,
}

我正在执行的查询是...(我只收到来自我的应用的 searchTerm)

clienteModel.aggregate([
        //in the first stage i find the client by searchTerm that contains the Name;
        {
            $match: { 
                $or: [                   
                    { name: { '$regex': req.body.searchTerm, '$options': 'i' } },
                ]
            }
        },
        //in the second stage i would like to receive only the Rate that match with the client.rate
        {
            $lookup: {
               from: "rates",
                as: "rate",
                pipeline: [
                   {
                       $match: {
                        _id: $rate
                       }
                   }
               ]
           }
      }
        //in the third stage, i would like to bring the configs that match with client.rate
        {
            $lookup: {
                from: "configs",
                as: "config",
                pipeline: [
                    {
                        $match: {
                            idRate: $rate
                        }
                    }
                ]
            }
        }

    ])

但是这个查询不起作用,它说 $rate 没有定义...我想我不知道它是如何工作的,我该如何声明 $rate?我认为如果 cliente.rate 是 clientModel 的属性,它是定义的......请帮助

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    你可以尝试如下:

    db.clienteModel.aggregate([
        {
            $match: { 
                    $or: [                   
                        { name: { '$regex': req.body.searchTerm, '$options': 'i' } },
                    ]
                }       
        },
        {
            $lookup: {
                from: "rate",
                let: { "rateId": "$rate" },
                pipeline: [
                    {
                        $match: {
                            $expr: { $eq: ["$_id", "$$rateId"] },
                        }
                    }
                ],
                as: "rateDetails"
            }
        },
        {
            $lookup: {
                from: "config",
                let: { "rateId": "$rate" },
                pipeline: [
                    {
                        $match: {
                            $expr: { $eq: ["$idRate", "$$rateId"] },
                        }
                    }
                ],
                as: "configDetails"
            }
        },
    ]);
    

    输出:

    {
        "_id" : ObjectId("5e7fb25ddc976375d3c224eb"),
        "name" : "Jhon",
        "rate" : ObjectId("5e7fb22fdc976375d3c224ea"),
        "rateDetails" : [
            {
                "_id" : ObjectId("5e7fb22fdc976375d3c224ea"),
                "title" : "Rate for sellers",
                "margin" : 10
            }
        ],
        "configDetails" : [
            {
                "_id" : ObjectId("5e7fb2a2dc976375d3c224ec"),
                "idRate" : ObjectId("5e7fb22fdc976375d3c224ea"),
                "maring" : 8
            }
        ]
    }
    

    【讨论】:

    • 非常感谢,您成功了!我必须研究更多关于聚合的知识,我认为我错过了一些概念......比如“让”来定义......再次感谢!
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 2020-09-22
    • 2017-02-16
    • 2020-08-07
    • 2020-08-06
    • 1970-01-01
    • 2022-07-09
    • 1970-01-01
    相关资源
    最近更新 更多