How to create a database and collection in MongoDB

MongoDB is a popular open-source, document-oriented NoSQL database. It is used for storing and retrieving data from a database. MongoDB is a powerful and flexible database that can be used for a variety of applications. In this tutorial, we will learn how to create a database and collection in MongoDB.

Install MongoDB

The first step in creating a database and collection in MongoDB is to install MongoDB. MongoDB can be installed on Windows, Mac, and Linux operating systems. To install MongoDB, you can download the MongoDB Community Server from the MongoDB website. Once you have downloaded the MongoDB Community Server, you can follow the instructions to install MongoDB on your system.

Start MongoDB

Once you have installed MongoDB, you can start the MongoDB server. To start the MongoDB server, you can use the mongod command. This command will start the MongoDB server on the default port (27017).

Connect to MongoDB

Once the MongoDB server is running, you can connect to the MongoDB server using the mongo command. This command will connect to the MongoDB server on the default port (27017).

Create a Database

Once you have connected to the MongoDB server, you can create a database. To create a database, you can use the use command. This command will create a new database with the specified name. For example, to create a database named “my_database”, you can use the following command:

use my_database

Once the database is created, you can use the show dbs command to view the list of databases.

Create a Collection

Once you have created a database, you can create a collection. A collection is a group of documents in MongoDB. To create a collection, you can use the db.createCollection() command. This command will create a new collection with the specified name. For example, to create a collection named “my_collection”, you can use the following command:

db.createCollection("my_collection")

Once the collection is created, you can use the show collections command to view the list of collections.

Insert Documents

Once you have created a collection, you can insert documents into the collection. A document is a record in MongoDB. To insert a document into a collection, you can use the db.collection.insert() command. This command will insert a new document into the specified collection. For example, to insert a document into the “my_collection” collection, you can use the following command:

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

Once the document is inserted, you can use the db.collection.find() command to view the documents in the collection.

Query Documents

Once you have inserted documents into a collection, you can query the documents. To query the documents, you can use the db.collection.find() command. This command will return the documents that match the specified query. For example, to query the documents in the “my_collection” collection with the name “John Doe”, you can use the following command:

db.my_collection.find({name: "John Doe"})

This command will return the documents that match the specified query.

Drop Database

Once you have created a database and collection, you can drop the database. To drop the database, you can use the db.dropDatabase() command. This command will drop the specified database. For example, to drop the “my_database” database, you can use the following command:

db.dropDatabase("my_database")

Once the database is dropped, you can use the show dbs command to view the list of databases.

Conclusion

In this tutorial, we have learned how to create a database and collection in MongoDB. We have also learned how to insert documents into a collection and query documents from a collection. Finally, we have learned how to drop a database.

Useful Links