आप बड़े संग्रह के लिए forEach() के साथ अपडेट कमांड का उपयोग कर सकते हैं। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं
>db.addingNewPropertyDemo.insertOne({"StudentName":"John","StudentAge":23,"CountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca1e61866324ffac2a7dc56")
}
>db.addingNewPropertyDemo.insertOne({"StudentName":"David","StudentAge":21,"CountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca1e62366324ffac2a7dc57")
}
>db.addingNewPropertyDemo.insertOne({"StudentName":"Bob","StudentAge":21,"CountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca1e62d66324ffac2a7dc58")
} खोज () विधि की सहायता से संग्रह से सभी दस्तावेज़ों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है
> db.addingNewPropertyDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5ca1e61866324ffac2a7dc56"),
"StudentName" : "John",
"StudentAge" : 23,
"CountryName" : "US"
}
{
"_id" : ObjectId("5ca1e62366324ffac2a7dc57"),
"StudentName" : "David",
"StudentAge" : 21,
"CountryName" : "AUS"
}
{
"_id" : ObjectId("5ca1e62d66324ffac2a7dc58"),
"StudentName" : "Bob",
"StudentAge" : 21,
"CountryName" : "UK"
} बड़े संग्रह में प्रत्येक दस्तावेज़ में नई संपत्ति जोड़ने की क्वेरी निम्नलिखित है
> db.addingNewPropertyDemo.find().forEach(function(data){ db.addingNewPropertyDemo.update({_id: data._id}, {$set: { StudentNameInUpperCase: data.StudentName.toUpperCase() }}) }); आइए देखें कि कोई नई संपत्ति जोड़ी गई है या नहीं
> db.addingNewPropertyDemo.find().pretty();
नई संपत्ति को प्रदर्शित करने वाला आउटपुट निम्नलिखित है
{
"_id" : ObjectId("5ca1e61866324ffac2a7dc56"),
"StudentName" : "John",
"StudentAge" : 23,
"CountryName" : "US",
"StudentNameInUpperCase" : "JOHN"
}
{
"_id" : ObjectId("5ca1e62366324ffac2a7dc57"),
"StudentName" : "David",
"StudentAge" : 21,
"CountryName" : "AUS",
"StudentNameInUpperCase" : "DAVID"
}
{
"_id" : ObjectId("5ca1e62d66324ffac2a7dc58"),
"StudentName" : "Bob",
"StudentAge" : 21,
"CountryName" : "UK",
"StudentNameInUpperCase" : "BOB"
} उपरोक्त नमूना आउटपुट को देखें, StudentNameInUpperCase गुण जोड़ा गया है।