mongodb array query code example
Example 1: mongodb find element in array
db.yourCollection.find( { array: "element" } )
Example 2: find string element in an array mongodb
db.articles.find({
"stock.country" : "01",
"stock.warehouse.code" : "02"
}).pretty();
Example 3: how to query array of object in mongoos
User.findOne({'local.rooms': {$elemMatch: {name: req.body.username}}}, function (err, user) {
if (err){
return done(err);
}
if (user) {
console.log("ROOM NAME FOUND");
req.roomNameAlreadyInUse = true;
next();
} else {
req.roomNameAlreadyInUse = false;
console.log("ROOM NAME NOT FOUND");
next();
}
});
Example 4: mongo array
db.inventory.find( { tags: ["red", "blank"] } )