mongodb query array code example

Example 1: query mongodb

db.inventory.find( { status: "D" } )

Example 2: mongodb find element in array

db.yourCollection.find( { array: "element" } )

Example 3: mongodb find array with element

// Finds all documents that have a property named "tags"
// which has at least one array element matching "red"
db.inventory.find( { tags: "red" } )

Example 4: query mongodb

db.inventory.find( {} )

Example 5: 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 6: mongo array

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

Tags:

Go Example