CRUD operations in MongoDB

You are here : Home / MongoDB Tutorial


Contents of page >

CRUD STANDS FOR >
C (CREATE),
R (READ),
U (UPDATE),
D (DELETE)


1) C (CREATE) in MongoDb >

1.1) Create new database in mongoDB



> use mydb
switched to db mydb
>


We created new database(if not exists) in mongoDB named mydb


1.2) Create new collection(table) in mongoDB

Use createCollection() method to create collection in mongoDB>
> db.createCollection("employeeTable")
{ "ok" : 1 }
>

Create new collection in different database in mongoDB>
> use testdb;
switched to db testdb
> db.createCollection("employeeTable")
{ "ok" : 1 }
>
First we switched to testdb, and then created collection in it.




2) R (READ) in MongoDb >

For details : READ - (Query/ display/ find/ search/ select) documents in collection in mongoDB



2.1) Query (read/ display/ find/ search/ select) document (record/row) in collection (table) in mongoDB >


We will use find method of mongoDB.


Let's create new collection and insert document in it before finding >
> db.employee.insert({_id : 1,  firstName:"ankit"})
> db.employee.insert({_id : 2,  firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3,  firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4,  firstName:"neh", salary : 3000 })
First line above will create table (or collection) (if table already exists it will insert documents in it).


FIND Example > Query all documents of collection using find() method>
> db.employee.find()
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
4 documents were found.


2.2) Display documents of collection in formatted manner.

First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1,  firstName:"ankit"})
> db.employee.insert({_id : 2,  firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3,  firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4,  firstName:"neh", salary : 3000 })
First line above will create table (or collection) (if table already exists it will insert documents in it).


FIND Example > Query all document of collection using find().pretty() method>
> db.employee.find().pretty()



1) FIND Example > Display only first 2 documents of collection in MongoDB >
We will use find() and limit() method>
> db.employee.find().limit(2)

FIND Example > Skip first document and display rest of documents of collection in MongoDB >
We will use find() and skip() method >
> db.employee.find().skip(1)


FIND Example > Display only 2nd and 3rd document of collection in MongoDB>
We will use find(), limit() and skip() method >
> db.employee.find().skip(1).limit(2)


FIND Example > Display only 2 documents of collection in MongoDB where salary >= 1000 >
> db.employee.find( { salary : {$gte : 1000} }).limit(2)

2.4) Find document from collection where field(column) EXISTS or not in collection in MongoDB



2.5) Sorting (order by) documents in MongoDB



2.6) EQUALS (=) and NOT EQUALS (!=) conditions - using $where, $eq and $ne operator in MongoDB



2.7) Projections in MongoDB - Selecting specific fields of documents in collection in MongoDB



2.8) Use AND and OR condition together > using $and operator and $or operator in MongoDB

2.9) LESS THAN (<) and LESS THAN EQUALS (<=) - using $where, $lt and $lte operator in MongoDB



2.10) GREATER THAN (>) and GREATER THAN EQUALS (>=) conditions - using $where, $gt and $gte operator in MongoDB



2.11) OR condition - using $or operator on document in collection in MongoDB



2.12) AND condition - using $and operator on document in collection in MongoDB



3) U (UPDATE) in MongoDb >

Read : Update documents in collection in mongoDB


3.1) Before updating let’s create and query collection in MongoDB


Let's create collection and insert documents in it before update >
> db.employee.insert({id : 1,  firstName:"ankit"})
> db.employee.insert({id : 2,  firstName:"sam"})
First line above will create table (or collection) (if table already exists it will insert documents in it).


Let's query/see what is there in collection before update >
> db.employee.find();
{ "_id" : ObjectId("584ebed11127dea5ece72742"), "id" : 1, "firstName" : "ankit" }
{ "_id" : ObjectId("584ebee21127dea5ece72743"), "id" : 2, "firstName" : "sam" }




3.2) UPDATE Example1 in MongoDB >
Let’s UPDATE firstname where id=1 >
We will use update method of mongoDB
> db.employee.update({id:1},{$set:{firstName:"ankit_UPDATED"}})


Now let's display documents of collection after update >
> db.employee.find();
{ "_id" : ObjectId("584ebed11127dea5ece72742"), "id" : 1, "firstName" : "ankit_UPDATED" }
{ "_id" : ObjectId("584ebee21127dea5ece72743"), "id" : 2, "firstName" : "sam" }


3.3) UPDATE Example2 in MongoDB >
Let’s UPDATE id where firstName= "sam" >
> db.employee.update({firstName:"sam"},{$set:{id:22}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : "sam" })


Now, let's display documents of collection after update >
> db.employee.find();
{ "_id" : ObjectId("584ebed11127dea5ece72742"), "id" : 1, "firstName" : "ankit_UPDATED" }
{ "_id" : ObjectId("584ebee21127dea5ece72743"), "id" : 22, "firstName" : "sam" }



4) D (DELETE) in MongoDb >


Read : Delete documents in collection (table) in mongoDB



4.1) Before deleting let’s create and query collection in MongoDB


Let's create collection and insert documents in it before delete >
> db.employee.insert({id : 1,  firstName:"ankit"})
> db.employee.insert({id : 2,  firstName:"sam"})
First line above will create table (or collection) (if table already exists it will insert documents in it).


Let's query/see what is there in collection before delete >
> db.employee.find();
{ "_id" : ObjectId("584ebed11127dea5ece72742"), "id" : 1, "firstName" : "ankit" }
{ "_id" : ObjectId("584ebee21127dea5ece72743"), "id" : 2, "firstName" : "sam" }


4.2) DELETE Example1 in MongoDB >
Let’s DELETE document where id=1 >
We will use remove method of mongoDB.
> db.employee.remove({id:1})
WriteResult({ "nRemoved" : 1 })
>


Now, let's display documents of collection after delete>
> db.employee.find();
{ "_id" : ObjectId("584ebee21127dea5ece72743"), "id" : 2, "firstName" : "sam" }
>
Only 1 document found in collection .

4.3) DELETE Example2 in MongoDB >
Let’s DELETE document where firstName= "sam" >
> db.employee.remove({firstName:"sam"})


Now, let's display documents of collection after delete>
> db.employee.find();
>
No document found in collection.


4.4) How to Delete all documents from employee collection in MongoDB>
> db.employee.remove({});


Let's query/see collection after deleting all documents >
> db.employee.find();
No, document was found.

Also read : How to Delete all documents from collection in MongoDB


SUMMARY>
So in this mongoDB tutorial we learned how to perform CRUD operations in MongoDB in MongoDb.
Use db.employee.drop() to drop collection in MongoDb.

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



Create new database in mongoDB



Create new collection(table) in mongoDB


eEdit
Must read for you :