Search code examples
node.jsmongodbmongoose

mongoose search by a property in a json object field


Let's say I have a schema as

var TempSchema = new Schema({
    location: Schema.Types.Mixed
});

location will store a json object

now I want to search by a property inside this json object field, can I use following query ?

Temp.find({location.country: {$in: ['US', 'CN', 'JP']}});

Solution

  • Yes you can do it using the dot notation, just enclose it inside quotes:

    Temp.find({"location.country": {$in: ['US', 'CN', 'JP']}}, function(err, data) { /* ... */});