繁体   English   中英

猫鼬发现或条件无法正常工作

[英]Mongoose find with or condition not working as expected

我想查找该字符串是否存在于shop_name或架构中food内部的name中。

这是查询

dbModel.stores.find({ "food.name": { '$regex': re } }).or([{ 'shop_name': { '$regex': re } }]).exec(function(err, docs) {
        if (!err) {
            res.status(200).json({
                "success": "1",
                "message": docs
            });
        } else {
            res.status(200).json({
                "success": "0",
                "message": "No result found"
            });
        }
    });

这是我的图式

var storeSchema = {
    "area": String,
    "food": [{
        'name': String,
        'description': String
    }],
    "shop_name": String
};

我想显示与shop_namefood.name的字符串匹配的记录

以上仅在字符串包含在两个元素中时给我记录。 我该如何查询or ,如果shop_name或food.name匹配,则应显示它

我认为您需要将条件放入$ or数组中。

因为food是一组对象,所以您可能需要在第一个条件下使用$ elemMatch

我以前从未做过,但是看起来像:

dbModel.stores.find({ 
    $or: [
        { 
           food: { 
               $elemMatch: { name: { $regex: re }}
           }
        }, 
        { 
           shop_name: { $regex: re }
        }
    ] 
}).exec(...)

另一个参考

暂无
暂无

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

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