How to insert document in MongoDB

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

The documents stored in MongoDB are JSON-like. All data stored into the collection are in BSON format.

Switch to a MongoDB database

 > use myinfo
switch to db myinfo

Define a document for MongoDB database

 > 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"]});

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"
        ]
}

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"]});

Insert data into a collection without defining a document

Data can be insert 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"
}
]       	   
});

Inclined to build a profession as Mongo DB Developer?
Then here is the blog post on Mongo DB Training.

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"
                }
        ]
}

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/1

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox