- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to query all items in MongoDB?
To query all items, use find(). Let us first create a collection with documents −
> db.queryAllItemsDemo.insertOne({"StudentDetails":{"StudentName":"John","StudentSubject":["MongoDB","MySQL"],"StudentSubjectPrice":[4000,6000]},"OtherDetails":{"UserAge":29,"UserCountryName":"US"}}); { "acknowledged" : true, "insertedId" : ObjectId("5cef74ecef71edecf6a1f69f") }
Display all documents from a collection with the help of find() method −
> db.queryAllItemsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cef74ecef71edecf6a1f69f"), "StudentDetails" : { "StudentName" : "John", "StudentSubject" : [ "MongoDB", "MySQL" ], "StudentSubjectPrice" : [ 4000, 6000 ] }, "OtherDetails" : { "UserAge" : 29, "UserCountryName" : "US" } }
Following is how you can query all items in MongoDB −
> db.queryAllItemsDemo.find({},{StudentDetails: 1}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cef74ecef71edecf6a1f69f"), "StudentDetails" : { "StudentName" : "John", "StudentSubject" : [ "MongoDB", "MySQL" ], "StudentSubjectPrice" : [ 4000, 6000 ] } }
Advertisements