0% completed
Read/query operations in MongoDB are essential for retrieving data from a collection. These operations allow you to find and manipulate documents based on specified criteria. MongoDB provides several methods for querying documents, each with its own set of features and use cases.
This lesson will cover the following methods:
find()
findOne()
findAndModify()
findOneAndDelete()
findOneAndUpdate()
findOneAndReplace()
.The find()
method is used to query documents in a collection that match the specified criteria. It returns a cursor to the documents that match the query. This method is highly flexible and allows for extensive querying capabilities, including filtering, projection, sorting, and pagination.
db.collection.find( <query>, <projection> )
Basic Query
db.users.find({ age: { $gte: 30 } })
This command retrieves all documents where the age
field is greater than or equal to 30.
Query with Projection
db.users.find({ age: { $gte: 30 } }, { name: 1, age: 1, _id: 0 })
This command retrieves all documents where the age
field is greater than or equal to 30 and returns only the name
and age
fields, excluding the _id
field.
The findOne()
method is used to query a single document that matches the specified criteria. It returns the first document that matches the query.
db.collection.findOne( <query>, <projection> )
Basic Query
db.users.findOne({ name: "Alice" })
This command retrieves the first document where the name
is "Alice".
Query with Projection
db.users.findOne({ name: "Alice" }, { name: 1, age: 1, _id: 0 })
This command retrieves the first document where the name
is "Alice" and returns only the name
and age
fields, excluding the _id
field.
The findAndModify()
method is used to modify and return a single document. It atomically modifies the document and returns it.
db.collection.findAndModify({ query: <document>, sort: <document>, remove: <boolean>, update: <document>, new: <boolean>, fields: <document>, upsert: <boolean> })
true
, removes the selected document.true
, returns the modified document rather than the original.true
, inserts a new document if no document matches the query.Modify and Return Document
db.users.findAndModify({ query: { name: "Bob" }, update: { $set: { age: 31 } }, new: true })
This command modifies the first document where the name
is "Bob", sets the age
to 31, and returns the modified document.
Remove and Return Document
db.users.findAndModify({ query: { name: "Charlie" }, remove: true })
This command removes the first document where the name
is "Charlie" and returns the removed document.
The findOneAndDelete()
method is used to delete a single document that matches the specified criteria and return the deleted document.
db.collection.findOneAndDelete( <filter>, <options> )
Delete and Return Document
db.users.findOneAndDelete({ name: "Alice" })
This command deletes the first document where the name
is "Alice" and returns the deleted document.
The findOneAndUpdate()
method is used to update a single document that matches the specified criteria and return the updated document.
db.collection.findOneAndUpdate( <filter>, <update>, <options> )
Update and Return Document
db.users.findOneAndUpdate( { name: "Bob" }, { $set: { age: 32 } }, { returnNewDocument: true } )
This command updates the first document where the name
is "Bob", sets the age
to 32, and returns the updated document.
The findOneAndReplace()
method is used to replace a single document that matches the specified criteria with a new document and return the replaced document.
db.collection.findOneAndReplace( <filter>, <replacement>, <options> )
Replace and Return Document
db.users.findOneAndReplace( { name: "Charlie" }, { name: "Charlie", age: 36, email: "charlie.new@example.com" }, { returnNewDocument: true } )
This command replaces the first document where the name
is "Charlie" with a new document and returns the new document.
.....
.....
.....