इसके लिए आप $push ऑपरेटर का इस्तेमाल कर सकते हैं। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं
>db.twoSeparateArraysDemo.insertOne({"StudentName":"Larry","StudentFirstGameScore":[98],"StudentSecondGameScore":[77]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9b152815e86fd1496b38b8")
}
>db.twoSeparateArraysDemo.insertOne({"StudentName":"Mike","StudentFirstGameScore":[58],"StudentSecondGameScore":[78]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9b152d15e86fd1496b38b9")
}
>db.twoSeparateArraysDemo.insertOne({"StudentName":"David","StudentFirstGameScore":[65],"StudentSecondGameScore":[67]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9b153315e86fd1496b38ba")
} खोज () विधि की सहायता से संग्रह से सभी दस्तावेज़ों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है
> db.twoSeparateArraysDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5c9b152815e86fd1496b38b8"),
"StudentName" : "Larry",
"StudentFirstGameScore" : [
98
],
"StudentSecondGameScore" : [
77
]
}
{
"_id" : ObjectId("5c9b152d15e86fd1496b38b9"),
"StudentName" : "Mike",
"StudentFirstGameScore" : [
58
],
"StudentSecondGameScore" : [
78
]
}
{
"_id" : ObjectId("5c9b153315e86fd1496b38ba"),
"StudentName" : "David",
"StudentFirstGameScore" : [
65
],
"StudentSecondGameScore" : [
67
]
} MongoDB में एक अपडेट कॉल में दो अलग-अलग सरणियों को पुश करने की क्वेरी निम्नलिखित है
> db.twoSeparateArraysDemo.update({_id:ObjectId("5c9b152d15e86fd1496b38b9")}, { $push : {
StudentFirstGameScore : 45, StudentSecondGameScore : 99}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) आइए देखें कि मान दो अलग-अलग सरणियों में धकेला गया है या नहीं
> db.twoSeparateArraysDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5c9b152815e86fd1496b38b8"),
"StudentName" : "Larry",
"StudentFirstGameScore" : [
98
],
"StudentSecondGameScore" : [
77
]
}
{
"_id" : ObjectId("5c9b152d15e86fd1496b38b9"),
"StudentName" : "Mike",
"StudentFirstGameScore" : [
58,
45
],
"StudentSecondGameScore" : [
78,
99
]
}
{
"_id" : ObjectId("5c9b153315e86fd1496b38ba"),
"StudentName" : "David",
"StudentFirstGameScore" : [
65
],
"StudentSecondGameScore" : [
67
]
}