Sub-documents के लिए dot notation का उपयोग करें। आइए पहले documents के साथ एक collection बनाते हैं:
> db.demo537.insertOne({"details":{"SubjectName":"MongoDB"}});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a10ef4dcbee04fbbc05") }
> db.demo537.insertOne({"details":{"SubjectName":"MySQL"}});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a4bef4dcbee04fbbc06") }
> db.demo537.insertOne({"details":{"SubjectName":"Java"}});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a51ef4dcbee04fbbc07") }
सभी Documents Display करना
> db.demo537.find();
Output:
{ "_id" : ObjectId("5e8c8a10ef4dcbee04fbbc05"), "details" : { "SubjectName" : "MongoDB" } }
{ "_id" : ObjectId("5e8c8a4bef4dcbee04fbbc06"), "details" : { "SubjectName" : "MySQL" } }
{ "_id" : ObjectId("5e8c8a51ef4dcbee04fbbc07"), "details" : { "SubjectName" : "Java" } }
Sub-documents पर Query Fire करना
Sub-document field पर query करने के लिए dot notation (details.SubjectName) का उपयोग करें:
> db.demo537.count({'details.SubjectName': 'MongoDB'})
Output:
1
मुख्य बिंदु: Dot notation MongoDB में nested fields को access करने का standard तरीका है। आप find(), count(), aggregate() आदि सभी operations में इसका उपयोग कर सकते हैं।