繁体   English   中英

如何在 mongodb 中使用正则表达式填充值

[英]how to use regex for populated values in mongodb

我正在使用 mongodb regex来获取过滤器中的类似数据。 但我不知道如何在同一个查询中使用regex填充数据。

Mongodb:

 assetAggregate = Assets.aggregate();
        assetAggregate.match({
                                company : new mongoose.Types.ObjectId(company),
                            })
                        .lookup({
                            from: "tickets",
                            localField: "_id",
                            foreignField: "assets",
                            as: "ticket_assets"
                        })
                        .lookup({
                            from:"locations",
                            localField:"location",
                            foreignField:"_id",
                            as:"locationData"
                        })
                        .unwind({ 'path': '$locationData', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"user_dropdowns",
                            localField:"category",
                            foreignField:"_id",
                            as:"categoryData"
                        })
                        .unwind( { 'path': '$categoryData', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"admin_dropdowns",
                            localField:"asset_status",
                            foreignField:"_id",
                            as:"assetStatusData"
                        })
                        .unwind( { 'path': '$assetStatusData', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"user_dropdowns",
                            localField:"condition",
                            foreignField:"_id",
                            as:"assetCondition"
                        })
                        .unwind( { 'path': '$assetCondition', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"stores",
                            localField:"deployedAt",
                            foreignField:"_id",
                            as:"deployedStore"
                        })
                        .unwind( { 'path': '$deployedStore', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"company_employees",
                            localField:"assignedObj.allottedTo",
                            foreignField:"_id",
                            as:"assignedEmployee"
                        })
                        .unwind( { 'path': '$assignedEmployee', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"locations",
                            localField:"using_location",
                            foreignField:"_id",
                            as:"assetUsingLocation"
                        })
                        .sort({"createdAt":-1})
                        .project({
                            "_id":1, "asset_name":1,"asset_code":1,
                            "status" : 1,"createdAt" : 1,"assetId": 1,
                            "location": {"$ifNull": ["$locationData", null]},
                            "category" : {"$ifNull": ["$categoryData", null]},
                            "asset_status" : {"$ifNull": ["$assetStatusData", null]},
                            "ticketCount": {"$size": "$ticket_assets"},
                            "deployedAt": {"$ifNull": ["$deployedStore", null]},
                            "assignedTo": {"$ifNull": ["$assignedEmployee", null]},
                            "condition": {"$ifNull": ["$assetCondition", null]},
                            "using_location": {"$ifNull": ["$assetUsingLocation", null]},
                            "predictive_maintenance": 1,
                            "maintenanceValidationData": 1,
                            "allocationValidationData": 1
                        })
                        .match({
                            "$or": [
                                { asset_name: { '$regex': payload.value, '$options': 'i' } },
                                { asset_code: { '$regex': payload.value, '$options': 'i' } },
                                { location:{name: { '$regex': payload.value, '$options': 'i' }} },
                            ]
                        });

在上面的查询中,我一直在填充位置数据,例如location:{ _id:xxxxxxxxx, name:"Guntur", .... } payload.value='Guntur'

现在我想根据有效载荷值获取数据。 我如何在match使用regex

替换为这一行 { "location.name": { '$regex': payload.value, '$options': 'i' }},

或者你可以使用。

{ "location.name": new RegExp(payload.value,'i')},

暂无
暂无

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

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