Search This Blog

Wednesday, December 25, 2013

How to find out how many documents were affected by the last instruction

Important note: This article is in relation to online MongoDB course. For more information about the course and other posts describing its content please check my main page here: M101P: MongoDB for Developers course

getLastError attribute

On mongo shell the data modification operation not always generate any readable output to show what happened. The below trick can be used to:
  • Find last operation status (success or failure).
  • Find number of rows affected or changed ( the 'n' parameter )
  • To find out if a new document was inserted during insert-update
  • To find out how many documents were updated
 > db.people.insert( { _id : "top", var : 2} )
E11000 duplicate key error index: students.people.$_id_  dup key: { : "top" }
> db.runCommand( {getLastError : 1 } )
{
        "err" : "E11000 duplicate key error index: students.people.$_id_  dup key: { : \"top\" }",
        "code" : 11000,
        "n" : 0,
        "connectionId" : 1,
        "ok" : 1
}

---

> db.people.insert( { _id : "mark", var : 3} )
> db.runCommand( {getLastError : 1 } )
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }

---

> db.people.update( {var : { $lt:3 } } , { $set : { var2: 0 } }, {multi: true} )
> db.runCommand( {getLastError : 1 } )
{
        "updatedExisting" : true,
        "n" : 2,
        "connectionId" : 2,
        "err" : null,
        "ok" : 1
}
> db.people.find()
{ "_id" : "mark", "var" : 3 }
{ "_id" : "rado", "var" : 1, "var2" : 0 }
{ "_id" : "top", "var" : 2, "var2" : 0 }

---

# showing bad example with wrong order of functions! 

> db.people.update( {var : 4} , { $set : { var2: 0 } }, { upsert: true } )
> db.people.find()
{ "_id" : "mark", "var" : 3 }
{ "_id" : "rado", "var" : 1, "var2" : 0 }
{ "_id" : "top", "var" : 2, "var2" : 0 }
{ "_id" : ObjectId("52ba26f9c1207a4a1fdeb3ae"), "var" : 4, "var2" : 0 }
> db.runCommand( {getLastError : 1 } )
{ "n" : 0, "connectionId" : 2, "err" : null, "ok" : 1 }

---

# the same operation but with correct functions order 

> db.people.update( {var : 4} , { $set : { var2: 0 } }, { upsert: true } )
> db.runCommand( {getLastError : 1 } )
{
        "updatedExisting" : false,
        "upserted" : ObjectId("52ba2916c1207a4a1fdeb3b0"),
        "n" : 1,
        "connectionId" : 2,
        "err" : null,
        "ok" : 1
}
> db.people.find()
{ "_id" : "mark", "var" : 3 }
{ "_id" : "rado", "var" : 1, "var2" : 0 }
{ "_id" : "top", "var" : 2, "var2" : 0 }
{ "_id" : ObjectId("52ba2916c1207a4a1fdeb3b0"), "var" : 4, "var2" : 0 }

---

> db.people.update( {} , { $set : { var2: 1 } }, { multi: true } )
> db.runCommand( {getLastError : 1 } )
{
        "updatedExisting" : true,
        "n" : 4,
        "connectionId" : 2,
        "err" : null,
        "ok" : 1
}
> db.people.find()
{ "_id" : "rado", "var" : 1, "var2" : 1 }
{ "_id" : "top", "var" : 2, "var2" : 1 }
{ "_id" : ObjectId("52ba26f9c1207a4a1fdeb3ae"), "var" : 4, "var2" : 1 }
{ "_id" : "mark", "var" : 3, "var2" : 1 }

No comments:

Post a Comment