How to insert document in MongoDB

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"

}

]

}

For indepth understanding click on