In MongoDB, updateOne, updateMany, and replaceOne are commonly used methods for
modifying documents within a collection.
Here’s a breakdown with examples to illustrate each:
1. updateOne()
This method updates a single document that matches the specified filter.
If multiple documents match, only the first one is updated.
Syntax:
db.collection.updateOne(filter, update, options)
Example: Assume we have a students collection with documents like:
{ "_id": 1, "name": "John", "age": 20, "grade": "B" }
{ "_id": 2, "name": "Alice", "age": 21, "grade": "A" }
{ "_id": 3, "name": "Bob", "age": 20, "grade": "C" }
To update only the first document where age is 20, changing the grade to "A":
db.students.updateOne({ age: 20 }, { $set: { grade: "A" } })
Result:
Only the document with _id: 1 will have its grade updated.
2. updateMany()
This method updates multiple documents that match the specified filter.
Syntax:
db.collection.updateMany(filter, update, options)
Example: To update all documents where age is 20, changing their grade to "B+":
db.students.updateMany({ age: 20 }, { $set: { grade: "B+" } })
Result: Both documents with _id: 1 and _id: 3 will have their grade changed to "B+".
3. replaceOne()
This method replaces a single document that matches the filter with an entirely new document.
Unlike updateOne, which modifies specific fields, replaceOne replaces the entire document,
except for the _id field.
Syntax:
db.collection.replaceOne(filter, replacement, options)
Example: To replace the document where _id is 2 with a new document:
db.students.replaceOne({ _id: 2 }, { name: "Alice", age: 22, grade: "A+" })
Result: The document with _id: 2 will now be:
{ "_id": 2, "name": "Alice", "age": 22, "grade": "A+" }.
In MongoDB, deleteOne and deleteMany are used to remove documents from a collection.
1. deleteOne()
This method deletes a single document that matches the specified filter. If multiple documents
match the filter, only the first one encountered is deleted.
Syntax:
db.collection.deleteOne(filter)
Example: Assume we have a products collection with the following documents:
{ "_id": 1, "name": "Laptop", "category": "Electronics" }
{ "_id": 2, "name": "Phone", "category": "Electronics" }
{ "_id": 3, "name": "Shirt", "category": "Clothing" }
To delete only the first document where the category is "Electronics":
db.products.deleteOne({ category: "Electronics" })
Result: The document with _id: 1 is deleted. The collection now looks like:
{ "_id": 2, "name": "Phone", "category": "Electronics" }
{ "_id": 3, "name": "Shirt", "category": "Clothing" }
---
2. deleteMany()
This method deletes all documents that match the specified filter.
Syntax:
db.collection.deleteMany(filter)
Example: To delete all documents where the category is "Electronics":
db.products.deleteMany({ category: "Electronics" })
Result: Both documents with _id: 1 and _id: 2 are deleted. The collection now looks like:
{ "_id": 3, "name": "Shirt", "category": "Clothing" }
In MongoDB, you can use various "create" operations to insert documents into collections.
Here are the main methods for creating or inserting data:
1. insertOne()
This method inserts a single document into a collection.
Syntax:
db.collection.insertOne(document)
Example: Assume you have a users collection. To insert a single document:
db.users.insertOne({ name: "Alice", age: 25, city: "New York" })
Result: The users collection now contains:
{ "_id": ObjectId("..."), "name": "Alice", "age": 25, "city": "New York" }
2. insertMany()
This method inserts multiple documents into a collection at once.
Syntax:
db.collection.insertMany([document1, document2, ...])
Example: To insert multiple documents into the users collection:
db.users.insertMany([
{ name: "Bob", age: 30, city: "San Francisco" },
{ name: "Charlie", age: 28, city: "Los Angeles" }
])
Result: The users collection now contains:
{ "_id": ObjectId("..."), "name": "Alice", "age": 25, "city": "New York" }
{ "_id": ObjectId("..."), "name": "Bob", "age": 30, "city": "San Francisco" }
{ "_id": ObjectId("..."), "name": "Charlie", "age": 28, "city": "Los Angeles" }.
Example 2: Retrieve documents with a filter: To find all students with age greater than 20:
db.students.find({ age: { $gt: 20 } })
Result:
{ "_id": 2, "name": "Bob", "age": 22, "grade": "B" }
{ "_id": 3, "name": "Charlie", "age": 21, "grade": "C" }.
In MongoDB, query criteria are used to filter and select documents based on specific conditions.
MongoDB provides a range of operators that can be combined to form complex queries.
Here’s a breakdown of commonly used query criteria:
1. Comparison Operators
These operators are used to compare values in documents.
$eq: Matches documents where the field is equal to a specified value.
db.collection.find({ age: { $eq: 25 } })
$ne: Matches documents where the field is not equal to a specified value.
db.collection.find({ age: { $ne: 25 } })
$gt: Matches documents where the field is greater than a specified value.
db.collection.find({ age: { $gt: 25 } })
$gte: Matches documents where the field is greater than or equal to a specified value.
db.collection.find({ age: { $gte: 25 } })
$lt: Matches documents where the field is less than a specified value.
db.collection.find({ age: { $lt: 25 } })
$lte: Matches documents where the field is less than or equal to a specified value.
db.collection.find({ age: { $lte: 25 } }).
In MongoDB, you can use $or, $and, and $in operators to perform complex queries. These
operators allow you to retrieve documents that match various combinations of conditions.
1. $or Operator
The $or operator retrieves documents that match at least one of the conditions specified.
Syntax:
db.collection.find({
$or: [
{ condition1 },
{ condition2 },
...
})
Example:
Retrieve all products that are either in the "Clothing" category or have a price less than 200.
db.products.find({
$or: [
{ category: "Clothing" },
{ price: { $lt: 200 } }
})
2. $and Operator
The $and operator retrieves documents that match all of the specified conditions.
Syntax:
db.collection.find({
$and: [
{ condition1 },
{ condition2 },
...
})
Example:
Retrieve all products that are in the "Electronics" category and have a price greater than 300.
db.products.find({
$and: [
{ category: "Electronics" },
{ price: { $gt: 300 } }
})
Note: MongoDB implicitly performs an $and when you specify multiple conditions directly
without $and.
db.products.find({ category: "Electronics", price: { $gt: 300 } })
---
3. $in Operator
The $in operator retrieves documents where the field’s value matches any of the values in the
specified array.
Syntax:
db.collection.find({
field: { $in: [value1, value2, ...] }
})
Example:
Retrieve all products that are either in the "Electronics" or "Clothing" category.
db.products.find({
category: { $in: ["Electronics", "Clothing"] }
})