MongoDB में अद्यतन दस्तावेज़ों की संख्या प्राप्त करने के लिए, आपको getlasterror के साथ runCommand का उपयोग करने की आवश्यकता है।
आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं
> db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca28c1d6304881c5ce84bad")
}
> db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca28c226304881c5ce84bae")
}
> db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Robert"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca28c276304881c5ce84baf")
}
> db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Ramit"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca28c366304881c5ce84bb0")
}
> db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Adam"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca28c436304881c5ce84bb1")
} खोज () विधि की सहायता से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है:
> db.getNumberOfUpdatedDocumentsDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{ "_id" : ObjectId("5ca28c1d6304881c5ce84bad"), "StudentName" : "David" }
{ "_id" : ObjectId("5ca28c226304881c5ce84bae"), "StudentName" : "Chris" }
{ "_id" : ObjectId("5ca28c276304881c5ce84baf"), "StudentName" : "Robert" }
{ "_id" : ObjectId("5ca28c366304881c5ce84bb0"), "StudentName" : "Ramit" }
{ "_id" : ObjectId("5ca28c436304881c5ce84bb1"), "StudentName" : "Adam" } दस्तावेज़ों को अद्यतन करने की क्वेरी निम्नलिखित है
> db.getNumberOfUpdatedDocumentsDemo.update({}, {$set : {"StudentName" : "Carol"}}, true, true);
WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 5 })
Now, get the number of updated documents:
> db.runCommand( "getlasterror" ); निम्नलिखित आउटपुट प्रदर्शित कर रहा है n =5 यानी 5 दस्तावेज़ अपडेट किए गए हैं
{
"connectionId" : 4,
"updatedExisting" : true,
"n" : 5,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
} अब संग्रह से सभी दस्तावेज़ प्रदर्शित करें
> db.getNumberOfUpdatedDocumentsDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{ "_id" : ObjectId("5ca28c1d6304881c5ce84bad"), "StudentName" : "Carol" }
{ "_id" : ObjectId("5ca28c226304881c5ce84bae"), "StudentName" : "Carol" }
{ "_id" : ObjectId("5ca28c276304881c5ce84baf"), "StudentName" : "Carol" }
{ "_id" : ObjectId("5ca28c366304881c5ce84bb0"), "StudentName" : "Carol" }
{ "_id" : ObjectId("5ca28c436304881c5ce84bb1"), "StudentName" : "Carol" }