MongoDB Select Query
This article will provide step by step basic concept of MongoDB.where you can easily understand the core concept of MongoDB.In a MongoDB<db.Collection.find()>method is used to return all document form collection.this method works same like select query in a relational database.you will find a single document and also find all document from the collection in pretty formate or non-structure.
Prerequisite
It would be better if you have the basic knowledge of RDBMS concept. Actually, you are going to learn a high-performance database so we need some basics of these.You should also read my previous articles.In which I explained.MongoDB inserts document step by step Detail and How can create a Database in MongoDB.
Implementation Section
- Find method().
- The find pretty Method().
- FindOne Method().
Find method()
To select all document from the database the find method is used.find method returns all the documents.the document shown in find method () is non-structure.
The following is basic Syntax-
1 | db.Name_of_collection.Find() |
Output:
The following data is not a formatted way.To overcome this problem we will use a pretty method.
The find pretty method ()
Using find pretty method().All the result are showing a formatted way. I will show you a simple example with syntax.
The following basic Syntax-
1 | db.Name_of_collection.find().pretty() |
The following data will display in a formatted way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | db.programmerhelper.find().pretty() { "_id" : ObjectId("59576700d29c06880a3ef933"), "isActive" : true, "balance" : "$3,818.97", "age" : 23, "eyeColor" : "green", "name" : "Oneill Everett", "gender" : "male", "company" : "INCUBUS", "email" : "oneilleverett@incubus.com", "phone" : "+1 (958) 522-2724", "address" : "273 Temple Court, Shelby, Georgia, 8682" } |
Using Pretty method everything is displayed in the document.
Output:
FindOne Method()
As the name implies FindOne method returns a single document.if you want to select a single document out of the whole then find one method() is used.
The following is basic Syntax
1 | db.name_of_collection.findOne() |
you can try this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | db.programmerhelper.findOne() { "_id" : ObjectId("59576700d29c06880a3ef933"), "isActive" : true, "balance" : "$3,818.97", "age" : 23, "eyeColor" : "green", "name" : "Oneill Everett", "gender" : "male", "company" : "INCUBUS", "email" : "oneilleverett@incubus.com", "phone" : "+1 (958) 522-2724", "address" : "273 Temple Court, Shelby, Georgia, 8682" } |
So searching the whole collection and return one document.
Output:
Conclusion
I wish I could tell you that a great site of MongoDB.you just understands key element above post-MongoDB select query working.Click here More Detail about how can find method work. I hope you will understand this lecture.Thank you for reading this lecture. Hope you got the idea. please share it.