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
]
}
}