MongoDB में किसी specific document को fetch करने के लिए find() मेथड में dot notation का उपयोग करें। आइए documents के साथ एक collection बनाते हैं:
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Oppo"}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5") }
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Samsung"}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea3eaa404263e90dac943e6") }
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"OnePlus"}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea3eacc04263e90dac943e7") }
सभी Documents Display करना
find() मेथड की मदद से collection के सभी documents display करें:
> db.demo672.find();
इससे निम्न output प्राप्त होगा:
{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] }
{ "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Samsung" } ] }
{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }
Specific Document Fetch करने का Query
Array element के आधार पर specific document fetch करने के लिए dot notation का उपयोग करें:
> db.demo672.find({"Brand.Name":"OnePlus"});
Output:
{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }
नोट: Dot notation (Brand.Name) का उपयोग करके आप nested array elements को directly query कर सकते हैं। यह MongoDB की powerful feature है जो complex data structures को query करने में मदद करती है।