MongoDB Querying for Data ( Getting Started ) Find()

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

retrieve all documents in a collection

db.collection.find({});

retrieve documents in a collection using a condition ( similar to WHERE in MYSQL )

db.collection.find({key: value}); 
example
  db.users.find({email:"[email protected]"});

retrieve documents in a collection using Boolean conditions (Query Operators)

//AND
db.collection.find( {
    $and: [
     { key: value }, { key: value } 
    ] 
})
//OR
db.collection.find( {
    $or: [
     { key: value }, { key: value } 
    ] 
})
//NOT
db.inventory.find( { key: { $not: value } } )

more boolean operations and examples can be found here

NOTE: find() will keep on searching the collection even if a document match has been found , therefore it is inefficient when used in a large collection , however by carefully modeling your data and/or using indexes you can increase the efficiency of find()



Got any MongoDB Question?