mongoose无效运算符$ match

时间:2017-02-20 18:51:44

标签: node.js mongodb mongoose mongodb-query aggregation-framework

我将根据email_address,firstname,lastname,type和phone_number处理用户搜索。

phone_number搜索将精确搜索&没有国家代码,而其他人将包含&不区分大小写的搜索。

所以,我写了下面的代码。

User.aggregate(
    [
        {
            "$redact": {
                "$cond": [
                    {
                        "$and": [
                            { 
                                "$match": {
                                    type: req.body.type,
                                    email_address: new RegExp((req.body.email_address || req.body.any || '').toLowerCase(), "i"),
                                    "firstname": new RegExp((req.body.firstname || req.body.any || '').toLowerCase(), "i") ,
                                    "lastname": new RegExp((req.body.lastname || req.body.any || '').toLowerCase(), "i") 
                                }
                            },
                            {
                                "$or": [
                                    {
                                        "$setIsSubset": [
                                            [
                                                { "$substr": [ "$phone_number.local_number", 0, -1 ] }
                                            ],
                                            [req.body.phone_number, req.body.any]
                                        ]
                                    },
                                    {
                                        "$setIsSubset": [
                                            [
                                                { 
                                                    "$concat": [ 
                                                        { "$substr": [ "$phone_number.country_code", 0, -1 ] },
                                                        { "$substr": [ "$phone_number.local_number", 0, -1 ] }
                                                    ] 
                                                }
                                            ],
                                            [req.body.phone_number, req.body.any]
                                        ]
                                    }, 
                                    {}            
                                ]

                            }
                        ]

                    },
                    "$$KEEP",
                    "$$PRUNE"
                ]
            }
        }
    ],
    function(err, users) {
        if (err) {
            return res.json({ success: false, err: err });
        }
        res.json({ success: true, users: users });
    }
);

但是当我运行此代码时,我得到“无效的运算符'$ match'”错误。

如果我删除$ match,它会将req.body值评估为表达式而不是值,并发出“FieldPath'abc'不以$开头”类错误。

所以,我希望得到帮助,如何解决这个问题并按条件搜索。

请帮帮我!!!

1 个答案:

答案 0 :(得分:1)

$match 移到 $redact 之外,因为它是一个独立的管道阶段,它会为初始过滤器提供正则表达式,否则可能是在 $redact 管道中无效:

User.aggregate([
    { 
        "$match": {
            "type": req.body.type,
            "email_address": new RegExp((req.body.email_address || req.body.any || '').toLowerCase(), "i"),
            "firstname": new RegExp((req.body.firstname || req.body.any || '').toLowerCase(), "i") ,
            "lastname": new RegExp((req.body.lastname || req.body.any || '').toLowerCase(), "i") 
        }
    },
    {
        "$redact": {
            "$cond": [
                {
                    "$or": [
                        {
                            "$setIsSubset": [
                                [ { "$substr": [ "$phone_number.local_number", 0, -1 ] } ],
                                [req.body.phone_number, req.body.any]
                            ]
                        },
                        {
                            "$setIsSubset": [
                                [
                                    { 
                                        "$concat": [ 
                                            { "$substr": [ "$phone_number.country_code", 0, -1 ] },
                                            { "$substr": [ "$phone_number.local_number", 0, -1 ] }
                                        ] 
                                    }
                                ],
                                [req.body.phone_number, req.body.any]
                            ]
                        }            
                    ]

                },
                "$$KEEP",
                "$$PRUNE"
            ]
        }
    }
], function(err, users) {
        if (err) {
            return res.json({ success: false, err: err });
        }
        res.json({ success: true, users: users });
    }
);