繁体   English   中英

如何匹配 mongoDB 查找数组中的值

[英]how to match value In mongoDB lookup array

这是结果数组,我想“$match” stepQuoteTool = true

{ 
    "quotes" : [
        {
            "_id" : ObjectId("5e0f02dc5023ec1de34e45bf"),               
            "firstName" : "Sagar", 
            "stepQuoteTool" : true, 
            "stepCarDetail" : true,            
        }
    ], 
    "_id" : ObjectId("5e0f02dc5023ec1de34e45be"), 
    "firstName" : "Sagar", 
    "createdAt" : ISODate("2020-01-03T09:01:16.748+0000"), 
    "device" : "Desktop", 
    "browser" : "Chrome", 
    "browserVersion" : "79.0.3945.88", 
    "os" : "Linux", 
    "screenSize" : "1920 X 383", 
}

以下是我的 mongodb 查询。 有人能帮忙吗? 我是 mongodb 的初学者

db.getCollection("tracks").aggregate([
{ $match: {'stepQuoteTool':true} },
{
                $lookup: {
                    from: 'quotes',
                    foreignField: 'track',
                    localField: '_id',
                    as: 'quotes'
                }
            },
            {
                $unwind: {
                    path: '$quotes',
                    preserveNullAndEmptyArrays: true
                }
            },
            {
                $group: {
                    _id: { _id: "$_id" },
                    firstName: { "$addToSet": "$firstName" },
                    quotes: { "$addToSet": "$quotes" },
                    createdAt: { "$addToSet": "$createdAt" },

                    device: { "$addToSet": "$device" },
                    browser: { "$addToSet": "$browser" },
                    browserVersion: { "$addToSet": "$browserVersion" },
                    os: { "$addToSet": "$os" },
                    screenSize: { "$addToSet": "$screenSize" },
                }
            }           
])

请给我可能的解决方案,谢谢。

db.getCollection("tracks").aggregate([
    {
        $lookup: {
            from: 'quotes',
            foreignField: 'track',
            localField: '_id',
            as: 'quotes'
        }
    }, {
        $project: {
            quotes: {
                $filter: {
                    input: "$quotes",
                    as: "data",
                    cond: {
                        $eq: ["$$data.stepQuoteTool", true]
                    }
                }
            }
        }
    }

])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM