MongoDB Operators
Fields Description
$currentDate à Sets the value of a
field to current date, either as a Date or a Timestamp.
$inc à Increments the value of the field by the
specified amount.
$min à Only updates the field
if the specified value is less than the existing field value.
$max à Only updates the field if the specified value
is greater than the existing field value.
$mul à Multiplies the value of the field by the
specified amount.
$rename à Renames a field.
$set à Sets the value of a
field in a document.
$setOnInsert à Sets the value of a
field if an update results in an insert of a document. Has no effect on update
operations that modify existing documents.
$unset à Removes the specified
field from a document.
Array Operators
$ Acts as a placeholder
to update the first element that matches the query condition in an update.
$addToSet à Adds elements to an
array only if they do not already exist in the set.
$pop à Removes the first or last item of an array.
$pull à removes all array
elements that match a specified query.
$pushAll à Deprecated. Adds
several items to an array.
$push à Adds an item to an array.
$pullAll à Removes all
matching values from an array.
Modifiers
$each à Modifies the $push and
$addToSet operators to append multiple items for array updates.
$position à Modifies the $push
operator to specify the position in the array to add elements.
$slice à Modifies the $push
operator to limit the size of updated arrays.
$sort à Modifies the $push
operator to reorder documents stored in an array.
Partial Update
$set Parameter
> db.sai.find()
{ "_id" :
ObjectId("59f034e02559518339882775"), "a" :
{ "_id" :
ObjectId("59f035092559518339882776"), "a" :
{ "_id" : 2, "a" :
42, "b" : "said", "dafa" : 232, "s
a=db.sai
a.insert({_id:1},{"name":"sai"})
$inc
> db.sai.insert({_id:3},{inc_sai:100})
WriteResult({ "nInserted" :
1 })
> db.sai.find({_id:3})
{ "_id" : 3 }
> db.sai.insert({_id:3},{$inc{inc_sai:100})
> db.sai.update({_id:3},{$inc:{inc_sai:1}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3,
"inc_sai" : 101 }
> db.sai.update({_id:3},{$inc:{inc_sai:1}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3,
"inc_sai" : 102 }
> db.sai.update({_id:3},{$inc:{inc_sai:5}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3,
"inc_sai" : 107 }
$push It will allows the
duplicate values
> a.update( { _id:1 } , {$push: { arr
:"hi"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> a.find({_id:1})
{ "_id" : 1,
"new" : 1, "arr" : [ "hi" ] }
> a.update( { _id:1 } , {$push: { arr
:"hi"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr
:"hi"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr
:"hi"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr
:"hi"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> a.find({_id:1})
{ "_id" : 1,
"new" : 1, "arr" : [ "hi", "hi", "Ihi",
"hi", "hi" ] }
$addToSet It prevents duplicate items from being
added to the array
> a.update( { _id:1 } , {$addToSet: { arr
:"bye"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$addToSet: { arr
:"bye"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 0 })
> a.update( { _id:1 } , {$addToSet: { arr
:"bye"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 0 })
> a.update( { _id:1 } , {$addToSet: { arr
:"bye"}})
WriteResult({ "nMatched" :
1, "nUpserted" : 0, "nModified" : 0 })
a.find({_id:1})
{ "_id" : 1,
"new" : 1, "arr" : [ "hi", "hi",
"hi", "hi", "hi", "bye" ] }
No comments:
Post a Comment