Tuesday, June 30, 2015

Update in MongoDB - More examples

Positional or field specific update
Use of $set ,$inc ,$unset operator
Updating multiple docs in single command ->{multi:1}
-by default {multi:0}
Upsert-> default is false


> db.numbers.insert({"x":10,"y":67})
WriteResult({ "nInserted" : 1 })
> db.numbers.insert({"x":10,"y":50})
WriteResult({ "nInserted" : 1 })
> db.numbers.insert({"x":10,"y":100})
WriteResult({ "nInserted" : 1 })
> db.numbers.find().pretty()
{ "_id" : ObjectId("559212cb8e94c66f57951518"), "x" : 10, "y" : 67 }
{ "_id" : ObjectId("559212d88e94c66f57951519"), "x" : 10, "y" : 50 }
{ "_id" : ObjectId("559212dd8e94c66f5795151a"), "x" : 10, "y" : 100 }


> db.numbers.update({"x":10}, {$set:{"y":120}},{multi:1} )
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.numbers.find().pretty()
{ "_id" : ObjectId("559212cb8e94c66f57951518"), "x" : 10, "y" : 120 }
{ "_id" : ObjectId("559212d88e94c66f57951519"), "x" : 10, "y" : 120 }
{ "_id" : ObjectId("559212dd8e94c66f5795151a"), "x" : 10, "y" : 120 }
>

Note : if you do not use $set , schema will change after you update
 Unset : to remove a column

> db.numbers.find().pretty()
{ "_id" : ObjectId("559212cb8e94c66f57951518"), "x" : 101, "y" : 202 }
{ "_id" : ObjectId("559212d88e94c66f57951519"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("559212dd8e94c66f5795151a"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("55921888ab28e968f573138f"), "x" : 101, "y" : 202 }

> db.numbers.update({"x":101}, {$unset:{"y":202}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.numbers.find().pretty()
{ "_id" : ObjectId("559212cb8e94c66f57951518"), "x" : 101 }
{ "_id" : ObjectId("559212d88e94c66f57951519"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("559212dd8e94c66f5795151a"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("55921888ab28e968f573138f"), "x" : 101, "y" : 202 }
>





  
Increment
> db.numbers.update({"x":10}, {$inc:{"y":5}},{multi:1} )
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.numbers.find().pretty()
{ "_id" : ObjectId("559212cb8e94c66f57951518"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("559212d88e94c66f57951519"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("559212dd8e94c66f5795151a"), "x" : 10, "y" : 125 }
>
 Upsert: to modify or insert a new document

> db.numbers.find().pretty()
{ "_id" : ObjectId("559212cb8e94c66f57951518"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("559212d88e94c66f57951519"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("559212dd8e94c66f5795151a"), "x" : 10, "y" : 125 }
> db.numbers.update({"x":10}, {$set:{"x":101,"y":202}},{upsert:1} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.numbers.find().pretty()
{ "_id" : ObjectId("559212cb8e94c66f57951518"), "x" : 101, "y" : 202 }
{ "_id" : ObjectId("559212d88e94c66f57951519"), "x" : 10, "y" : 125 }
{ "_id" : ObjectId("559212dd8e94c66f5795151a"), "x" : 10, "y" : 125 }

> db.numbers.update({"x":1}, {$set:{"x":101,"y":202}},{upsert:1} )
WriteResult({
        "nMatched" : 0,
        "nUpserted" : 1,
        "nModified" : 0,
        "_id" : ObjectId("55921888ab28e968f573138f")
})
>





No comments:

Post a Comment