उप-दस्तावेज़ मूल्य तक पहुँचने के लिए, आइए पहले हम दस्तावेज़ों के साथ एक संग्रह बनाएँ -
> db.accessSubDocumentDemo.insertOne(
... {
...
... "Details" : {
... "1" : {
... "StudentLowerScore" : "33",
... "StudentHoghScore" : "55"
... },
... "2" : {
... "StudentLowerScore" : "45",
... "StudentHoghScore" : "65"
... },
... "3" : {
... "StudentLowerScore" : "39",
... "StudentHoghScore" : "91"
... },
... "4" : {
... "StudentLowerScore" : "41",
... "StudentHoghScore" : "85"
... }
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd3baf0edc6604c74817cd6")
} खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.accessSubDocumentDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : ObjectId("5cd3baf0edc6604c74817cd6"),
"Details" : {
"1" : {
"StudentLowerScore" : "33",
"StudentHoghScore" : "55"
},
"2" : {
"StudentLowerScore" : "45",
"StudentHoghScore" : "65"
},
"3" : {
"StudentLowerScore" : "39",
"StudentHoghScore" : "91"
},
"4" : {
"StudentLowerScore" : "41",
"StudentHoghScore" : "85"
}
}
} अब, जब कुंजी एक संख्या है, तो हम उप-दस्तावेज़ मूल्य तक पहुँच प्राप्त करेंगे:यहाँ, उप दस्तावेज़ को संख्या 1 के साथ कुंजी के लिए एक्सेस किया जाता है -
> db.accessSubDocumentDemo.findOne().Details["1"];
यह निम्नलिखित आउटपुट देगा -
{ "StudentLowerScore" : "33", "StudentHoghScore" : "55" }