आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
>db.demo11.insertOne({"ListOfStudent":[{"StudentName":"Chris","ListOfScore":[76,67,54,89]}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e0f6e34d7df943a7cec4fa1")
} खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.demo11.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{
"_id" : ObjectId("5e0f6e34d7df943a7cec4fa1"),
"ListOfStudent" : [
{
"StudentName" : "Chris",
"ListOfScore" : [
76,
67,
54,
89
]
}
]
} यहाँ एक शर्त के साथ एक सरणी तत्व सम्मिलित करने की क्वेरी है -
> db.demo11.update( {"ListOfStudent.StudentName":"Chris"}, {$push:{"ListOfStudent.$.ListOfScore":98}} );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) आइए एक बार फिर से दस्तावेजों की जांच करें -
> db.demo11.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{
"_id" : ObjectId("5e0f6e34d7df943a7cec4fa1"),
"ListOfStudent" : [
{
"StudentName" : "Chris",
"ListOfScore" : [
76,
67,
54,
89,
98
]
}
]
}