array query mongodb code example

Example 1: mongodb array field contains

db.inventory.find( { tags: ["red", "blank"] } )

Example 2: mongodb array field contains

db.inventory.find( { tags: { $all: ["red", "blank"] } } )

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();

        }

    });

Tags:

Misc Example