CRUD Operations in MongoDB

Hello All, After a long time writing this article, many people asked about CRUD operations of MongoDB.
CRUD Operations- CRUD Operations stands for Creation, Reading, Update and Deletion of Database.
I am giving information regarding how can we create Database, Collection (aka table in RDBMS World), insert data in collection, then read the collection, will do some find query, then update and deletion of documents.
1. Creation Of Database & Collection
To create a database:-
$ mongo
MongoDB Enterprise > use demo;
switched to db demo
MongoDB Enterprise > db
demo
Now, if you see above, I have connected to mongo shell from Linux terminal, then use database name demo. Here if we don’t have database called “demo” then it will create a database named “demo“, else it will use demo database considering it’s an existing database.
To create a collection and insert some record:- 
If we think about RDBMS world, then creation of table means you have to define schema of table and record which you want to insert should be same as of data-type defined during table creation.
But MongoDB give us flexibility, we don’t have to bother about the schema of collection, you want to insert a document (record in RDBMS), just insert don’t have to define that collection and their data-type. Let’s look at below example.
MongoDB Enterprise > db.firstSession.insert({ "article" : "CRUD Operation", created_at : new Date(), author : "Mukesh Kumar", domain : "blogfordba.org" })
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > 
MongoDB Enterprise > db.firstSession.insert({ "article" : "Profiling in MongoDB", created_at : new Date(), author : "Mukesh Kumar", domain : "blogfordba.org" })
WriteResult({ "nInserted" : 1 })
I have inserted 1 record in collection “firstSession“. If we see above, I have not defined any schema, not data-type.
Let’s look for the document.
MongoDB Enterprise > db.firstSession.findOne()
{
 "_id" : ObjectId("57ab77ffcbfc6713771fd376"),
 "article" : "CRUD Operation",
 "created_at" : ISODate("2016-08-10T18:52:47.917Z"),
 "author" : "Mukesh Kumar",
 "domain" : "blogfordba.org"
}
We can see 1 document is there in the collection “firstSession“.
2. Read Operations in MongoDB:-
To read a collection or fetch document from a collection, we will use find query and then where condition like we use in RDBMS database.
MongoDB Enterprise > db.firstSession.find({ author : "Mukesh Kumar" }).pretty()
{
 "_id" : ObjectId("57ab77ffcbfc6713771fd376"),
 "article" : "CRUD Operation",
 "created_at" : ISODate("2016-08-10T18:52:47.917Z"),
 "author" : "Mukesh Kumar",
 "domain" : "blogfordba.org"
}
{
 "_id" : ObjectId("57ab7940cbfc6713771fd377"),
 "article" : "Profiling in MongoDB",
 "created_at" : ISODate("2016-08-10T18:58:08.187Z"),
 "author" : "Mukesh Kumar",
 "domain" : "blogfordba.org"
}
As we can see, I find an author where name is “Mukesh Kumar”, search in MongoDB is case sensitive, if you want to search in any case use regular expression. I will give another article on that.
3. Update in MongoDB:- 
We can update a document as given below-
db.firstSession.update({ author : "Mukesh Kumar" }, { $set : { domain : "www.blogfordba.org" } }, false, true )
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Let me explain, how update of a document works-
I did search operation first( like where clause in RDBMS), and then setting(updating) domain to “www.blogfordba.org“. Next I have mentioned false and then true.
First false indicate, if search document not found in the collection, then don’t insert a record in the collection with domain : “www.blogfordba.org”. If it’s true then it will insert.
Second true indicate, if it’s true update all matching search documents, if it’s false it will update only 1 document.
4. Deletion in MongoDB:- 
We can delete a document, delete or drop complete collection or a database.
Delete a document:- 
To delete a document, you can choose you want to delete a document or many document.
MongoDB Enterprise > db.firstSession.remove({ author : "Mukesh Kumar" }, { justOne : true } )
WriteResult({ "nRemoved" : 1 })
Here in above, justOne is indicating, we want to remove only one document, if you want to remove all matching documents, then omit the justOne and by default it’s false.
MongoDB Enterprise > db.firstSession.remove({ author : "Mukesh Kumar" } )
WriteResult({ "nRemoved" : 2 })
Delete all documents from a collection:-
MongoDB Enterprise > db.firstSession.remove({ } )
WriteResult({ "nRemoved" : 2 }) 
To Drop a collection in MongoDB:-
MongoDB Enterprise > db.firstSession.drop()
true
To Drop Database in MongoDB:-
MongoDB Enterprise > db.dropDatabase()
{ "dropped" : "demo", "ok" : 1 }

No comments:

Post a Comment