MongoDB Tutorials

MongoDB Create A Collection Tutorial with Example

In this mongoDB tutorial, we are going to learn how to create a collection in MongoDB database using MongoDB shell.

In MongoDB, the first step is to create a collection so that we can store all the data into the collections. Collection refers to a `key`: `value` pair JSON data file which holds the user information like name, email, age, etc.

There are 2 methods offered by mongoDB to create collection. Let’s understand those 2 methods through which we can create a collection in mongoDB:

Understand MongoDB createCollection() Method

Creating a collection in mongoDB is very simple using the given below method. This method helps you create a data collection without inserting a document in mongoDB.

The Syntax

db.createCollection(collection_name, options)

Understand createcollection() method a bit more:

Argument Details
collection_name The first argument is the name of the collection in the database.
options It’s an optional parameter however you can pass the collection options like: capped, autoIndexId, size etc.

In the next, step we’ll check some of the options for managing the collection in mongoDB.

Options Type Details
capped boolean It’s an optional value when you set it to `true` it creates a capped collection. The capped collection is also known as the fixed size data collection, and it overwrites the oldest entries when it reached the max size limit. If you set it to true, you should also set a maximum size in the size field.
autoIndexId boolean If you set it to false, then you can disable the auto-creation of an index on the _id field. Note: it was deprecated since version 3.2.
size number This value should only be used with the capped collection, and the size field is referred to the maximum size in bytes for a capped collection.
max number It’s an optional value, the max field in mongoDB defines the maximum number of documents passed in a capped collection.
validator document The validator field let us define the validation rule for the data collection. Validation can only be applied when updating and inserting the data in mongoDB.

For more details you can visit MongoDB official documentation here.

Check Databases in MongoDB

Run command to check the total number of database in your local system, although we are using `mongodatabase`.

show databases

# mongodatabase
# database2
# database3
# database4

Using MongoDB Database

Run below command to select mongoDB database using MongoDB shell:

use mongodatabase
use mongodatabase

# switched to db mongodatabase

Create a Collection in MongoDB

We are creating a collection using createCollection() mongoDB method without the options:

> db.createCollection("contacts")
{ "ok" : 1 }

Check MondoDB Collection

Run command to check the existing collection in the mongoDB.

$ show collections
contacts

Creating a Collection using MongoDB insert() Method

In this method, let’s find out how to easily create a collection by inserting a record in the collection. Remember, if the collection doesn’t exist, then the new collection will be created.

The Syntax

db.collection.insert()
db.users.insert(
   {
     "userid" : 1,
     "name" : "John"
   }
)

After executing the command, following output will be displayed in your terminal:

WriteResult({ "nInserted" : 1 })

Execute the command to check out the newly created collection which wasn’t available earlier.

> show collections
contacts
users
Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.

Recent Posts

React Redux Handle API Calls with Thunk Middleware Tutorial

In this post, we will learn how to work with HTTP requests in the Redux…

1 day ago

Node AJAX Retrieve Records from MySQL Database Tutorial

MySQL is a relational database management system based on SQL – Structured Query Language, and…

4 days ago

React Manage REST API State Globally with Context API Tutorial

React Js Handle Rest API data globally with Context and useState hook tutorial. In this…

1 week ago

How to Delete Data from MySQL Database using Node Js

Node js delete data from MySQL database tutorial; Throughout this guide, you will ascertain how…

1 week ago

Node Import CSV File Data to MySQL Database with HTML Form

In this tutorial, you will discover how to import CSV file into MySQL database using…

2 weeks ago

How to Handle Global State in React js using Context API

React Js Global state management with createContext hook example; In this tutorial, we will help…

2 weeks ago