Tutorialspoint

1 Answer
Nishtha Thakur

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

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.