mongodb array find code example

Example 1: mongodb find with gt

{field: {$gt: value} }

Example 2: mongodb findmany

model.find({
    '_id': { $in: [
        mongoose.Types.ObjectId('4ed3ede8844f0f351100000c'),
        mongoose.Types.ObjectId('4ed3f117a844e0471100000d'), 
        mongoose.Types.ObjectId('4ed3f18132f50c491100000e')
    ]}
}, function(err, docs){
     console.log(docs);
});

Example 3: mongodb array field contains

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

Example 4: 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 5: mongodb array field contains

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

Tags:

Go Example