w3resource logo
MongoDB Tutorial

Insert data in MongoDB

Description

In this page, we are going to discuss how to insert data into a collection. The documents stored in MongoDB are JSON-like. All data stored in the collection are in BSON format.

Switch to a MongoDB database

Here, our database is "myinfo".

> use myinfo
switch to db myinfo 

Define a document for MongoDB database

The following document can be stored in MongoDB.

> document=({"user_id" : "ABCDBWN","password" :"ABCDBWN" ,"date_of_join" : "15/10/2010" ,"education" :"B.C.A." , "profession" : "DEVELOPER","interest" : "MUSIC","community_name" :["MODERN MUSIC", "CLASSICAL MUSIC","WESTERN MUSIC"],"community_moder_id" : ["MR. BBB","MR. JJJ","MR MMM"],"community_members" : [500,200,1500],"friends_id" : ["MMM123","NNN123","OOO123"],"ban_friends_id" :["BAN123","BAN456","BAN789"]});

Document written in command prompt.

Defined document shows in command prompt

{
        "user_id" : "ABCDBWN",
        "password" : "ABCDBWN",
        "date_of_join" : "15/10/2010",
        "education" : "B.C.A.",
        "profession" : "DEVELOPER",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. BBB",
                "MR. JJJ",
                "MR MMM"
        ],
        "community_members" : [
                500,
                200,
                1500
        ],
        "friends_id" : [
                "MMM123",
                "NNN123",
                "OOO123"
        ],
        "ban_friends_id" : [
                "BAN123",
                "BAN456",
                "BAN789"
        ]
}

The document shows in command prompt

Insert a document into a collection

To save the above document into the collection "userdetails" under "myinfo" database the following command can be used -

> db.userdetails.insert(document) 

Write document using linebreak

Linebreaks can also be used while typing a document. This can be useful when writing a lengthy document as shown bellow:

>document=({"user_id" : "ABCDBWN","password" :"ABCDBWN" ,"date_of_join" : "15/10/2010" ,
"education" :"B.C.A." , "profession" : "DEVELOPER","interest" : "MUSIC",
"community_name" :["MODERN MUSIC", "CLASSICAL MUSIC","WESTERN MUSIC"],
"community_moder_id" : ["MR. BBB","MR. JJJ","MR MMM"],
"community_members" : [500,200,1500],"friends_id" : ["MMM123","NNN123","OOO123"],
"ban_friends_id" :["BAN123","BAN456","BAN789"]});

Document written in command prompt.

Insert data into a collection without defining a document

Data can be inserted directly through the shell without defining the document -

>db.userdetails.insert({"user_id" : "xyz123","password" :"xyz123" ,"date_of_join" : "15/08/2010" ,
"education" :"M.C.A." , "profession" : "Software consultant","interest" : "Film",
"community" : [
{
"name" : "DDD FILM CLUB",
"moder_id" : "MR. DBNA",
"members" : "25000",
},
{
"name" : "AXN MOVIES",
"moder_id" : "DOGLUS HUNT",
"members" : "15000",
},
{
"name" : "UROPEAN FILM LOVERS",
"moder_id" : "AMANT LUIS",
"members" : "20000",
}
],
"friends" :[
{
"user_id" : "KKK258",
},
{
"user_id" : "LLL147",
},
{
"user_id" : "MMM369",        	   
}
],
"ban_friends" :[
{
"user_id" : "BAN147"      	   
},       	   
{
"user_id" : "BAN258"
},
{
"user_id" : "BAN369"
}
]       	   
});

Command written in command prompt.

View the inserted data into the collection

>db.userdetails.find();
{
        "user_id" : "xyz123",
        "password" : "xyz123",
        "date_of_join" : "15/08/2010",
        "education" : "M.C.A.",
        "profession" : "Software consultant",
        "interest" : "Film",
        "community" : [
                {
                        "name" : "DDD FILM CLUB",
                        "moder_id" : "MR. DBNA",
                        "members" : "25000"
                },
                {
                        "name" : "AXN MOVIES",
                        "moder_id" : "DOGLUS HUNT",
                        "members" : "15000"
                },
                {
                        "name" : "UROPEAN FILM LOVERS",
                        "moder_id" : "AMANT LUIS",
                        "members" : "20000"
                }
        ],
        "friends" : [
                {
                        "user_id" : "KKK258"
                },
                {
                        "user_id" : "LLL147"
                },
                {
                        "user_id" : "MMM369"
                }
        ],
        "ban_friends" : [
                {
                        "user_id" : "BAN147"
                },
                {
                        "user_id" : "BAN258"
                },
                {
                        "user_id" : "BAN369"
                }
        ]
}

Document written in command prompt.