Insert in collection(table) in mongoDB

You are here : Home / MongoDB Tutorial


Contents of page >


1) Insert in collection(table) in mongoDB>


Note : (Table and collection are same thing in mongoDB),
(Record/Row and document are same thing in mongoDB)

1.1) We will use insert method of mongoDB.
> db.employee.insert({_id : 1,  firstName:"ankit"})
WriteResult({ "nInserted" : 1 })
>
Above line will create table (or collection) (if table already exists it will insert documents in it).


Let’s understand meaning of statement
WriteResult({ "nInserted" : 1 })
"nInserted" : 1  shows that 1 document was inserted successfully in  collection.


1.2) Insert another document in collection >
> db.employee.insert({_id : 2,  firstName:"sam"})
WriteResult({ "nInserted" : 1 })
>


1.3) Now, let's display documents of collection >
> db.employee.find();
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "sam" }
2 documents were found.


Here _id is the primary key.


Note 1 : (Table and collection are same thing in mongoDB),
(Record/Row and document are same thing in mongoDB)
Note 2 : You may also use db.employee.save(document), to insert documents.


2) In MongoDB document in collection is that each document can have different fields (columns) >


2.1) Insert another document in collection (But with different fields (columns) ) >
> db.employee.insert({_id : 3,  firstName:"neh", salary : "1000" })
WriteResult({ "nInserted" : 1 })
>

Now, let's display documents of  collection >
> db.employee.find();
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "sam" }
{ "_id" : 3, "firstName" : "neh", "salary" : "1000" }
3 documents were found.


2.2) Again, Insert another document in  collection (But with different fields (columns) ) >
> db.employee.insert({id : 4})
WriteResult({ "nInserted" : 1 })
>

Now, let's display documents of collection >
> db.employee.find();
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "sam" }
{ "_id" : 3, "firstName" : "neh", "salary" : "1000" }
{ "_id" : 4 }
4 documents were found.

3) Insert in collection(table) in mongoDB without _id field ( _id field will be formed automatically) >


We will use insert method of mongoDB.
> db.employee.insert({ firstName:"jas"})
WriteResult({ "nInserted" : 1 })
>


Now, let's display documents of collection >
> db.employee.find();
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "sam" }
{ "_id" : 3, "firstName" : "neh", "salary" : "1000" }
{ "_id" : ObjectId("588e43ec3c1047e53fc9458b"), "firstName" : "jas" }
{ "_id" : 4 }
5 documents were found.


If we don’t insert _id field. Then field _id is formed automatically. Its value is ObjectId("588341b303b0c717da77d641")


Read more about objectId in MongoDB

4) SUMMARY>
So in this mongoDB tutorial we learned how to Insert in collection(table) in mongoDB.
1) Insert in collection(table) in mongoDB>
1.1) We will use insert method of mongoDB.
> db.employee.insert({_id : 1,  firstName:"ankit"})


2) In MongoDB document in collection is that each document can have different fields (columns) >
2.1) Insert another document in collection (But with different fields (columns) ) >
> db.employee.insert({_id : 3,  firstName:"neh", salary : "1000" })


3) When we insert in collection(table) in mongoDB without _id field ( _id field will be formed automatically).

Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

What is MongoDB - A quick introduction to database

objectId in MongoDB

Create new database in mongoDB

Create new collection(table) in mongoDB

Data Modelling in MongoDB - Multiple tables


Labels: MongoDB
eEdit
Must read for you :