
The most significant element of MongoDB is the CRUD operations. CRUD stands for Create, Read, Update, and Delete, as you may know. In this post, we’ll go through how to insert a document in MongoDB, view a document in MongoDB, update a document in MongoDB, and remove a document in MongoDB.
Before reading this article we would suggest you to get basic information about Documents, Collections, Installation of MongoDB and difference between RDBMS and MongoDB.
Click here to learn the basics of MongoDB, Installation, MongoDB data types
Table of Contents
Insert documents in MongoDB
The insert operation core part of CRUD Operations in MongoDB. MongoDB has insert() or save() methods that you can use if you want to insert documents into a collection. The following is the syntax:
Insert document using insert() method in MongoDB
To insert a document into the database, use the insert() method. If the collection does not currently exist, the insert operation will create the collection.
> db.<collection_name>.insert(document)
There are examples of insert document using insert() method in MongoDB which are as follows:
> db.dept.insert({"deptno" : 10, "dname" : "ACCOUNTING", "loc" : "NEW YORK"}) > db.dept.insert({"deptno" : 20, "dname" : "RESEARCH", "loc" : "DALLAS"}) > db.dept.insert({"deptno" : 30, "dname" : "SALES", "loc" : "CHICAGO"}) > db.dept.insert({"deptno" : 40, "dname" : "OPERATIONS", "loc" : "BOSTON"})

Insert document using save() method in MongoDB
As you can see from the syntax and example of the insert() function above. You can also use the save() method to insert a document into MongoDB in addition to insert().
> db.dept.save({"deptno" : 10, "dname" : "ACCOUNTING", "loc" : "NEW YORK"}) > db.dept.save({"deptno" : 20, "dname" : "RESEARCH", "loc" : "DALLAS"}) > db.dept.save({"deptno" : 30, "dname" : "SALES", "loc" : "CHICAGO"}) > db.dept.save({"deptno" : 40, "dname" : "OPERATIONS", "loc" : "BOSTON"})

View Documents in MongoDB
The view operation is an important aspect of any database since it allows us to retrieve information from the database by using a query. The find() method in MongoDB is used to view a collection’s records.
> db.<collection_name>.find()

Insert Multiple Documents in MongoDB
If you wish to store a large amount of data in a database. In other words, if you need to insert more than one record at a time, mongodb has a capability that lets you to do so.
Syntax to Insert Multiple Documents
Here’s an syntax of inserting multiple documents at the same time.
> db.dept.insert([ { Document 1... }, { Document 2... }, { Document 3... }, { Document 4... } ])
Example for inserting multiple documents in MongoDB
> db.dept.insert([ {"deptno" : 10, "dname" : "ACCOUNTING", "loc" : "NEW YORK"}, {"deptno" : 20, "dname" : "RESEARCH", "loc" : "DALLAS"}, {"deptno" : 30, "dname" : "SALES", "loc" : "CHICAGO"}, {"deptno" : 40, "dname" : "OPERATIONS", "loc" : "BOSTON"} ])

Update Document in MongoDB
To update a document in a collection in MongoDB, use the update() and save() methods. To update a document, use the following syntax.
> db.<collection_name>.insert(selection_criteria, updated_data) > db.<collection_name>.save({_id:ObjectId(), new_data})
The update() and save() functions are used to update a document in the following manner:
> db.dept.update({'deptno': 10},{$set:{'loc':'KOLKATA'}}) > db.dept.save({"_id" : ObjectId("60505578791820eac9f650b2"), "deptno" : 20, "dname" : "RESEARCH",'loc':'MUMBAI'})
The main key _id should be included if you're using the save() method.

Delete Documents in MongoDB
The remove() method in MongoDB is used to delete documents from a collection. There are two arguments to the delete() function, both of which are optional. The two arguments are listed below:
- deletion criteria: Here we use the criteria to delete the document. Deletion criteria is nothing but a kind of condition.
- justOne: You must use true or 1 if you want to delete only one document in the criteria.
Syntax for Deleting documents in MongoDB
> db.<collection_name>.remove(deletion_criteria) > db.<collection_name>.remove(deletion_criteria, 1) > db.<collection_name>.remove({})
Deleting only one Document
> db.dept.remove({ deptno : 40 }, 1)

Delete all documents that meet a set of criteria
When you need to delete all documents with some criteria you can use it in following way. You have to use remove() method in which you need to specify criteria.
> db.dept.remove({ loc : "BOSTON" })

Deleting all Documents
Sometimes you may need to remove all documents at once, then in MongoDB you can use remove() method with empty parentheses. Here’s an example of deleting all documents.
> db.dept.remove({})

Query Document in MongoDB
The find() method in MongoDB is used to get all documents from a collection.
> db.<collection_name>.find()

limit(<number>) is used with the find() method to obtain a specific number of documents from a MongoDB collection.
> db.<collection_name>.find().limit(<number>)
Retrieve two Documents
> db.dept.find().limit(2)

Count documents in MongoDB
> db.<collection_name>.count() > db.dept.count()

Document sorting in MongoDB
The sort() method in MongoDB is used to sort documents. If you want to sort in ascending order you have to use 1 and if you want to sort in descending order you have to use -1. We will see in the example below.
Sorting in Ascending Order
In order to sort in ascending order, you will need to use the following syntax.
> db.<collection_name>.find().sort({KEY : 1})
> db.<collection_name>.find().sort({deptno : 1})

Sorting in Descending Order
In order to sort in descending order, you will need to use the following syntax.
> db.<collection_name>.find().sort({KEY : -1})
> db.<collection_name>.find().sort({deptno : -1})

Skip Documents in MongoDB
The skip(<number>) method in MongoDB is used to skip a document. We use skip() method in combination with the find() and limit() methods.
> db.<collection_name>.find().skip(<number>)
> db.<collection_name>.find().limit().skip(<number>)
Example for Skipping documents in MongoDB
We’ve given you five examples that show you how to skip documents.
> db.dept.find().skip(1) > db.dept.find().skip(2) > db.dept.find().skip(3) > db.dept.find().skip(5) > db.dept.find().limit().skip(2)

Hope you liked this article. In this article we have covered almost all the basic functions like Insert, Update, View, Delete, Count etc. This may help you to understand CRUD operations in MongoDB
If you have any doubts related to MongoDB documents and CRUD operations, you can comment here or you can contact us on contact page.