MongoDB is a popular NoSQL database that uses a document-oriented data model. One of the key advantages of MongoDB is its flexible query language, which enables users to query data in a variety of ways.
Once your database is up and running, You can use the following method to login to the database. In case you do not know how to create your own database on cloud for free then please refer to my previous post here.
Open the MongoDB shell: You can open the MongoDB shell by running the mongo
command in your terminal or command prompt.
Switch to the user of the database having admin rights:
In the MongoDB shell, you need to switch to the admin
database by running the command use admin
We will cover following queries in this section of the page.
1. db.collectionName.insertOne()
2. db.collectionName.insertMany()
3. db.collectionName.updateOne()
4. db.collectionName.updateMany()
5. db.collectionName.deleteOne()
6. db.collectionName.deleteMany()
- Inserting a new record into a collection: “insertOne” method in Mongo DB is used to insert one (single) record into a collection. The syntax for the
"insertOne()"
method is as follows:db.collectionName.insertOne(<document>,
{writeConcern: <document>})
* Here, “collectionName” is the name of the collection where the document is to be inserted.
* “<document>” is the json document to be inserted.
* ThewriteConcern
parameter is optional and specifies the level of acknowledgment requested from MongoDB for write operations.
Let’s understand this with an example.
Suppose we have a collection namedusers
, and we want to insert a new user document into it with the following fields:
{
"name": "John",
"age": 30,
"email": "john@example.com"
}
We can use the insertOne()
method as follows:
db.users.insertOne({
"name": "John",
"age": 30,
"email": "john@example.com"
})
This will insert a new record into the “users” collection.
2. To insert multiple documents (records) at once, we can use another method called “insertMany“.
The syntax for the insertMany()
method is as follows:
db.collection.insertMany(
[ <document 1> , <document 2>, ..., <document N> ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
Here, db.collection is the name of the collection where the documents are to be inserted, and [ <document 1> , <document 2>, ..., <document N> ]
is an array of documents to be inserted.
The writeConcern parameter is optional and specifies the level of acknowledgment requested from MongoDB for write operations. The ordered
parameter is also optional and specifies whether the documents should be inserted in the order specified in the array.
Example below:
Suppose we have a collection named users, and we want to insert multiple user documents into it with the following fields:
[
{
"name": "John",
"age": 30,
"email": "john@example.com"
},
{
"name": "Mary",
"age": 25,
"email": "mary@example.com"
},
{
"name": "Peter",
"age": 35,
"email": "peter@example.com"
}
]
We can use the insertMany() method as follows:
db.users.insertMany([
{
"name": "John",
"age": 30,
"email": "john@example.com"
},
{
"name": "Mary",
"age": 25,
"email": "mary@example.com"
},
{
"name": "Peter",
"age": 35,
"email": "peter@example.com"
}
])
This will insert the documents into the users collection.
3. Updating a existing record in a collection: “updateOne()
” is a method in the MongoDB database management system that is used to update a single document that matches a specified filter in a collection. The syntax for the updateOne()
method is as follows:
db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>
}
)
Here, db.collection
is the name of the collection where the document is to be updated, <filter>
is a query that matches the document to be updated, and <update>
is the modification to be made to the document.
The upsert
parameter is optional and specifies whether to insert a new document if a matching document is not found. The writeConcern
parameter is also optional and specifies the level of acknowledgment requested from MongoDB for write operations.
Example:
Suppose we have a collection named users
, and we want to update the age of the user named “John” to 31. We can use the updateOne()
method as follows:
db.users.updateOne(
{ "name": "John" },
{ $set: { "age": 31 } }
)
This will update the age of the user named “John” to 31 in the users
collection.
Note that the $set
operator is used to modify the value of the age
field. If the document with the specified filter is not found, no update will be made unless the upsert
parameter is set to true
. If multiple documents match the specified filter, only the first matching document will be updated.
4. To update multiple documents (records) in one go, we can use updateMany method in Mongo DB.
The syntax for the updateMany()
method is as follows:
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>
}
)
Here, db.collection
is the name of the collection where the documents are to be updated, <filter>
is a query that matches the documents to be updated, and <update>
is the modification to be made to the documents.
The upsert
parameter is optional and specifies whether to insert a new document if a matching document is not found. The writeConcern
parameter is also optional and specifies the level of acknowledgment requested from MongoDB for write operations.
Example:
Suppose we have a collection named users
, and we want to update the age of all users whose name starts with “J” to 31. We can use the updateMany()
method as follows:
db.users.updateMany(
{ "name": /^J/ },
{ $set: { "age": 31 } }
)
This will update the age of all users whose name starts with “J” to 31 in the users
collection.
Note that the $set
operator is used to modify the value of the age
field. If no documents match the specified filter, no update will be made unless the upsert
parameter is set to true
. If multiple documents match the specified filter, all matching documents will be updated.
Here is a video that explains the above concept in easy to understand manner.
5 The deleteOne()
method in MongoDB is used to delete a single document that matches the specified filter. It accepts a filter object as an argument, which defines the criteria for selecting the document to delete. If multiple documents match the filter, deleteOne()
only deletes the first document it finds.
Here’s an example of using deleteOne()
method to delete a document from a collection named users
where the name
field equals to “John”:
db.users.deleteOne({name: "John"})
This will delete the first document that matches the filter, which has a name
field equal to “John”. If you want to delete all documents that match the filter, you can use the deleteMany()
method instead.
Note that deleteOne()
method returns a DeleteResult
object that contains information about the operation, including the number of documents deleted.
6. The deleteMany()
method in MongoDB is used to delete all documents that match the specified filter. It accepts a filter object as an argument, which defines the criteria for selecting the documents to delete.
Here’s an example of using deleteMany()
method to delete all documents from a collection named users
where the status
field equals to “inactive”:
db.users.deleteMany({status: "inactive"})
This will delete all documents that match the filter, which have a status
field equal to “inactive”. If you want to delete only one document that matches the filter, you can use the deleteOne()
method instead.
Note that deleteMany()
method returns a DeleteResult
object that contains information about the operation, including the number of documents deleted.
Here is a video explaining all of the above in an easy to understand way.