Mongo DB
Mongo DB
1. updateOne()
This method updates a single document that matches the specified filter.
Syntax:
To update only the first document where age is 20, changing the grade to "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:
Example: To update all documents where age is 20, changing their grade to "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:
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)
Result: The document with _id: 1 is deleted. The collection now looks like:
---
2. deleteMany()
This method deletes all documents that match the specified filter.
Syntax:
db.collection.deleteMany(filter)
Result: Both documents with _id: 1 and _id: 2 are deleted. The collection now looks like:
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()
Syntax:
db.collection.insertOne(document)
2. insertMany()
This method inserts multiple documents into a collection at once.
Syntax:
db.users.insertMany([
])
Example 2: Retrieve documents with a filter: To find all students with age greater than 20:
Result:
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.
1. Comparison Operators
These operators are used to compare values in documents.
$ne: Matches documents where the field is not equal to a specified value.
$gt: Matches documents where the field is greater than a specified value.
$gte: Matches documents where the field is greater than or equal to a specified value.
$lt: Matches documents where the field is less than a specified value.
$lte: Matches documents where the field is less than or equal to a specified value.
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" },
})
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" },
})
Note: MongoDB implicitly performs an $and when you specify multiple conditions directly
without $and.
---
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({
Example:
Retrieve all products that are either in the "Electronics" or "Clothing" category.
db.products.find({
})