Wednesday 17 May 2017

Mongoose find with or condition not working as expected

I want to find whether the string exist either in the shop_name or name inside the food in the schema.

Here is the query

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"
            });
        }
    });

Here's my schema

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

I want to show the record which matches with the string either in the shop_name or food.name

The above gives me the record only if the string contains in both the element. How can i get check with or i.e., If shop_name or food.name was matched then it should display it



via SA__

No comments:

Post a Comment