How to insert document in MongoDB
AuthorTekslate
Ratings
Views0
  • 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 : 228

Vote for difficulty

Current difficulty :

Previous

Stack Class in Java

Next

Stack Class in Java

Recommended Articles

Page :

How to define Variables in Development Environment - Blue Prism
Views500
Blue Prism RealWorld UseCases
Views500
Blue Prism Deployment in productions
Views500
How companies are using Blue Prism for their Business
Views500
How companies are using Blue Prism for their Business
Views500
What is Blue Prism?
Views500

Recommended Programs
logoforcourse
How to define Variables in Development Environment - Blue Prism
4.5
25695 Learners
logoforcourse
Blue Prism RealWorld UseCases
4.5
25695 Learners

1/15

About Author
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 .

TABLE OF CONTENT


What is Blue Prism?
Difference between Blue Prism and UiPath
Blue Prism Benefits
Blue Prism Projects
Blue Prism Deployment in productions
How to define Variables in Development Environment - Blue Prism
Blue Prism Benefits and Uses in RealTime
Blue Prism RealWorld UseCases
Blue Prism RealWorld UseCases
Learn more about Blue Prism

Stay Updated


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

Related Blogs

Blue Prism Benefits and Uses in RealTime

Blue Prism RealWorld UseCases

How companies are using Blue Prism for their Business

Latest Updates in Blue Prism