
Part 4 — MongoDB Step by Step— Some More Basic Operations
In previous article of this series, we have talked about data types supported by MongoDB, also document insertion command. In this article we will see some more command related to documents (Fetch, Update, and Delete).
Let’s begin the mongo magic
So we inserted out documents successfully in last articles, now it’s time to fetch/retrieve/query those articles.
Find()
Find() method will fetch all the documents present in selected collection of database. In our case example case, it will fetch all the documents from Post collection of articles database.
db.Post.find().pretty() //pretty() method is for displaying result JSON in more readable way.

There is findOne() method also available with will return only one document.
Where clause in MongoDB

AND clause in MongoDB
OR clause in MongoDB
OK…But what if we have to update document?
MongoDB’s update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.
Suppose we have to update a document from out Post collection, which has title “Article Title”, and we want to change it to “Article Updated”. Command would be like:

By default MongoDB updates only one document which have that title, but what if there are multiple document with same title?
For updating multiple document we have to enable multi:true in previous command.
Delete Document
MongoDB provide remove() method, which is use to remove document(s) from collection. It accepts two input:
- deletion criteria − (Optional) deletion criteria according to documents will be removed.
- onlyOne − (Optional) if set to true or 1, then remove only one document.
Command would be like:

I am closing this part here. In next part we will develop REST API using Nodejs and MongoDB. Also we will use that API in android.
Thanks for being till here. If you find this helpful, be sure to click ❤ recommend. It means a lot.
