Open In App

How to Create Database and Collection in MongoDB

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB stores data records as documents that are stored together in collections and the database stores one or more collections of documents.

In this article, we will look at methods to create a database and collection in MongoDB.

Create a MongoDB Database

To create a database in MongoDB use the “use” command.

Syntax

use database_name

Example

We have created a MongoDB database named gfgDB, as shown in the image below:

creating mongodb database

Here, we have created a database named as “gfgDB”. If the database doesn’t exist, MongoDB will create the database when you store any data to it.

Note:  The "use" command is also used to switch to another database.

View all the Existing Databases

To view all existing databases in MongoDB use the “show dbs” command.

Syntax

show dbs

This command returns a list of all the existing MongoDB databases.

show dbs command output

To check your current database use the “db” command

Create a Collection in MongoDB

To create a collection in MongoDB use the createCollection() method. Collection name is passed as argument to the method.

Syntax

db.createCollection(‘ collection_name’ );

Example

Creating collection named “Student” in MongoDB

db.createCollection('Student');
create collection in MongoDB
createCollection() in MongoDB

Explanation:

In the above example, Student Collection is created using createCollection() method.

Note: In MongoDB, a  collection is also  created  directly when we add one or more documents to it. 

Insert Documents in Collection in MongoDB

We can insert documents in the collection using the two methods:

  • insertOne()
  • insertMany()

Let’s understand each of these methods with examples.

insertOne() Method

insertOne() method is used to insert only one document in the collection.

Example:

db.myNewCollection1.insertOne( { name:"geeksforgeeks" } )

Here, we create a collection named as “myNewCollection1” by inserting a document that contains a “name” field with its value in it using insertOne() method.

insertone() method in mongodb example

insertMany() method

insertMany() method is used to insert many documents in the collection

Example:

db.myNewCollection2.insertMany([{name:"gfg", country:"India"},
                                {name:"rahul", age:20}])

Here, we create a collection named as myNewCollection2 by inserting two documents using insertMany() method.

insertmany method in mongodb example

View all the Existing Collections in a MongoDB Database

To view all existing collections in a MongoDB database use the “show collection command“:

Syntax

show collections

Example

This command returns a list of all the existing collections in the gfgDB database.

show collections command output in mongodb


Previous Article
Next Article

Similar Reads

MongoDB - Database, Collection, and Document
Databases, collections, documents are important parts of MongoDB without them you are not able to store data on the MongoDB server. A Database contains a collection, and a collection contains documents and the documents contain data, they are related to each other. Database In MongoDB, a database contains the collections of documents. One can creat
6 min read
How to sort collection of MongoDB Database in ascending order using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This format of storage is called BSON ( similar to JSON f
2 min read
How to sort collection of MongoDB Database in descending order using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This format of storage is called BSON ( similar to JSON f
2 min read
How to Copy a Collection From One Database to Another in MongoDB?
Effective data management is crucial for MongoDB-dependent applications. As databases grow, the need to transfer collections between databases arises. This can be for creating development or testing environments, migrating data, or archiving historical information. MongoDB provides built-in capabilities and tools to simplify this process, offering
6 min read
How to create new Collection in MongoDB using Node.js ?
MongoDB the most popular NoSQL database, is an open-source document-oriented database. The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This format of storage is called BSON (similar to JSON for
1 min read
How to create new Mongodb database using Node.js ?
mongodb module: This Module is used to performing CRUD(Create Read Update Read) Operations in MongoDb using Node.js. We cannot make a database only. We have to make a new Collection to see the database. The connect() method is used for connecting the MongoDb server with the Node.js project. Please refer this link for connect() method. Installing mo
1 min read
Create Database using MongoDB Compass
MongoDB is a database that utilizes a document-oriented approach to store data. It internally stores data in BSON format, but as developers, we can interact with MongoDB using JSON for sending and receiving data. MongoDB handles the conversion between JSON and BSON automatically. In contrast, traditional relational databases like MySQL store data i
3 min read
How to Create a MongoDB Dump of Database?
MongoDB is a popular NoSQL database known for its flexibility and scalability. However, like any database system, it is crucial to have a backup plan in place to protect our data from loss or corruption. One of the best ways to back up your MongoDB database is by creating a dump. In this article, we will go through the process of creating a MongoDB
6 min read
MongoDB - Create Database using Mongo Shell
A MongoDB Database is the container for all the collections, where the Collection is a bunch of MongoDB documents similar to tables in RDBMS and the Document is made up of fields similar to a tuple in RDBMS, but it has a dynamic schema here. This article explains the easy method to create a Database in MongoDB using Mongo Shell. How to Create a Mon
2 min read
Defining, Creating and Dropping a MongoDB collection
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. DocumentIt is the basic unit of MongoDB and a single reco
2 min read
Article Tags :