Search This Blog

Wednesday, December 25, 2013

Modifying documents in the data base

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

Document updates

The update syntax is similar to how find function works. The first argument is a search document, the second is the NEW DOCUMENT to be inserted to REPLACE the existing.
 
> db.grades.find( { "student_id" : 1})
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57d"), "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57e"), "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }
>
> db.grades.update( { "student_id" : 1}, { myvar : "new doc2"} )
>

> db.grades.find( { $or : [ { "student_id" : 1 }, { myvar : "new doc2"} ] } )
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "myvar" : "new doc2" }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57d"), "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57e"), "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }

Adding or modifying new attribute in the document
 
> db.grades.find( { myvar : "new doc2"} )
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "myvar" : "new doc2" }
>
> db.grades.update( { myvar : "new doc2"}, { $set : { newvar : 1} } )
>
> db.grades.find( { myvar : "new doc2"} )
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "myvar" : "new doc2", "newvar" : 1 }

You can increase an existing attribute or add a new one if it doesn't.
 
> db.grades.update( { myvar : "new doc2"}, { $inc : { newvar : 100} } )
>
> db.grades.find( { myvar : "new doc2"} )
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "myvar" : "new doc2", "newvar" : 101 }
>
> db.grades.update( { myvar : "new doc2"}, { $inc : { newnumber : 2} } )
>
> db.grades.find( { myvar : "new doc2"} )
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "myvar" : "new doc2", "newnumber" : 2, "newvar" : 101 }

As MongoDB don't have a strict db schema we can dynamically remove an attribute as well.
 
> db.grades.update( { myvar : "new doc2"}, { $unset : { newnumber : 2} } )
> db.grades.find( { myvar : "new doc2"} )
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "myvar" : "new doc2", "newvar" : 101 }

If a value is an array you can update a individual element of the array in the document.
 
> db.arrays.insert( { a : 1, tab : [ 11,22,33] } )
> db.arrays.find()
{ "_id" : ObjectId("52ba1c78a83c1ee5e6c903a1"), "a" : 1, "tab" : [  11,  22,  33 ] }
>

> db.arrays.update( { a: 1}, { $set : { "tab.2" : "hello" } } )
> db.arrays.find()
{ "_id" : ObjectId("52ba1c78a83c1ee5e6c903a1"), "a" : 1, "tab" : [  11,  22,  "hello" ] }

There are other methods you can use. A complete list of them can be found at http://docs.mongodb.org/manual/reference/operator/update-array

No comments:

Post a Comment