Update or insert a new record into a document if it doesn't exist.
> db.arrays.find() { "_id" : ObjectId("52ba1c78a83c1ee5e6c903a1"), "a" : 1, "tab" : [ 11, 22, "hello" ] } > db.arrays.update( { a: 2}, { $set : { "tab.2" : "london" } } ) > db.arrays.find() { "_id" : ObjectId("52ba1c78a83c1ee5e6c903a1"), "a" : 1, "tab" : [ 11, 22, "hello" ] } > db.arrays.update( { a: 2}, { $set : { "tab.2" : "london" } }, { upsert: true} ) > db.arrays.find() { "_id" : ObjectId("52ba1c78a83c1ee5e6c903a1"), "a" : 1, "tab" : [ 11, 22, "hello" ] } { "_id" : ObjectId("52ba1e4cc1207a4a1fdeb3ac"), "a" : 2, "tab" : { "2" : "london" } } > db.arrays.update( { a: 3}, { $set : { "mystr" : "poland" } }, { upsert: true} ) > db.arrays.find() { "_id" : ObjectId("52ba1c78a83c1ee5e6c903a1"), "a" : 1, "tab" : [ 11, 22, "hello" ] } { "_id" : ObjectId("52ba1e4cc1207a4a1fdeb3ac"), "a" : 2, "tab" : { "2" : "london" } } { "_id" : ObjectId("52ba1e99c1207a4a1fdeb3ad"), "a" : 3, "mystr" : "poland" }
No comments:
Post a Comment