Create collection(table) and insert document(rows) into the collection in Mongodb

Create collection(table) and insert document(rows) into the collection in Mongodb

1. check the db which is connected.

db

2. connect with mydb database

use mydb

3. create two documents named j and k by using the following sequence of javascript

j = { name:”RAM”}
k = {address:”delhi”}
i = {x:3}

4. insert the  j, k and i into the collection name employee

db.employee.insert(j)
db.employee.insert(k)
db.employee.insert(i)

5. check the collection exists

show collections

6. confirm the document insert into the collection

db.employee.find()

Example

> use mydb
switched to db mydb

> show dbs
admin  (empty)
local  0.078GB
test   (empty)

> j={name:”RAM”}
{ “name” : “RAM” }
> k={address:”Delhi”}
{ “address” : “Delhi” }
> i={empno:10}
{ “empno” : 10 }

> db.employee.insert(j)
WriteResult({ “nInserted” : 1 })
> db.employee.insert(k)
WriteResult({ “nInserted” : 1 })
> db.employee.insert(i)
WriteResult({ “nInserted” : 1 })

> show collections
employee
system.indexes

> db.employee.find()
{ “_id” : ObjectId(“540eb24efbf4b1a1447adde9”), “name” : “RAM” }
{ “_id” : ObjectId(“540eb25ffbf4b1a1447addea”), “address” : “Delhi” }
{ “_id” : ObjectId(“540eb267fbf4b1a1447addeb”), “empno” : 10 }

> show dbs
admin  (empty)
local  0.078GB
mydb   0.078GB
test   (empty)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.