How to insert, update, and delete documents in MongoDB

MongoDB is a popular open-source NoSQL database that is used for storing and managing data. It is a document-oriented database, which means that it stores data in documents instead of tables. MongoDB is a powerful and flexible database that can be used to store and manage data for a variety of applications. In this tutorial, we will learn how to insert, update, and delete documents in MongoDB.

Install MongoDB

The first step in using MongoDB is to install it. MongoDB can be installed on a variety of operating systems, including Windows, Mac OS X, and Linux. The installation process is different for each operating system, so please refer to the MongoDB documentation for instructions on how to install MongoDB on your system.

Connect to MongoDB

Once MongoDB is installed, you can connect to it using a MongoDB client. There are a variety of MongoDB clients available, including the MongoDB shell, which is a command-line interface for interacting with MongoDB. To connect to MongoDB using the MongoDB shell, open a terminal window and type the following command:

mongo

This will connect to the default MongoDB instance running on your system. If you need to connect to a different instance, you can specify the hostname and port number as follows:

mongo --host --port

Once you are connected to MongoDB, you can start working with it.

Create a Database

The first step in working with MongoDB is to create a database. To create a database, use the use command followed by the name of the database you want to create. For example, to create a database named “mydb”, you would type the following command:

use mydb

Once the database is created, you can start working with it.

Insert a Document

Once you have created a database, you can start inserting documents into it. To insert a document into a collection, use the db.collection.insert() method. This method takes a document as an argument and inserts it into the specified collection. For example, to insert a document into the “users” collection, you would type the following command:

db.users.insert({name: "John Doe", age: 30})

This will insert a document into the “users” collection with the name “John Doe” and the age “30”.

Update a Document

To update a document in MongoDB, use the db.collection.update() method. This method takes two arguments: a query object and an update object. The query object is used to specify which documents should be updated, and the update object is used to specify what should be updated. For example, to update the age of the user “John Doe” to “31”, you would type the following command:

db.users.update({name: "John Doe"}, {$set: {age: 31}})

This will update the age of the user “John Doe” to “31”.

Delete a Document

To delete a document in MongoDB, use the db.collection.remove() method. This method takes a query object as an argument and deletes all documents that match the query. For example, to delete the user “John Doe”, you would type the following command:

db.users.remove({name: "John Doe"})

This will delete the user “John Doe” from the “users” collection.

Conclusion

In this tutorial, we have learned how to insert, update, and delete documents in MongoDB. We have also seen how to create a database and connect to it using a MongoDB client. MongoDB is a powerful and flexible database that can be used to store and manage data for a variety of applications.

Useful Links