The following common update operations are tested using mongodb2.6.3. The official stable version 2.4 is recommended. 1. upsert indicates that if there is data, it will not be inserted, and if there is no data, it will be inserted 1. the db under the command line. les. update (search for users whose name is tank... {name: tank },... {... _ id: 1 ,... name:
The following common update operations are tested using mongodb2.6.3. The official stable version 2.4 is recommended. 1. upsert indicates that if there is data, it will not be inserted, and if there is no data, it will be inserted 1. the db under the command line. les. update (// search for users whose name is tank... {name: "tank "},... {... "_ id": 1 ,... name:"
The following common update operations are tested using mongodb2.6.3. The official stable version 2.4 is recommended.
1. upsert indicates that if data exists, it will not be inserted. If no data exists, it will be inserted.
1. Under the command line
> Db. les. update (// search for users whose name is tank... {name: "tank "},... {... "_ id": 1 ,... name: "Andy ",... rating: 10 ,... score: 10 ...},... {upsert: true} // insert if no ...); writeResult ({"nMatched": 0, "nUpserted": 1, "_ id": 1})> db. les. find (); {"_ id": 1, "name": "Andy", "rating": 10, "score": 10}> db. les. update (... {_ id: 1 },... {... "_ id": 1 ,... name: "Andy ",... rating: 10 ,... score: 10 ...},... {upsert: true }...); writeResult ({"nMatched": 1, "nUpserted": 0}) // If matched data exists, no insert operation is performed.> db. les. find (); {"_ id": 1, "name": "Andy", "rating": 10, "score": 10}
2. php upsert operation
$collection->update( array("name" => "zhang"), array("_id"=>2,"name"=>"tank","rating"=>10,"score"=>10), array("upsert" => true));print_r($collection->findOne());
2. $ set replacement value
1. operations under the command line
> db.peoples.find();{ "_id" : 1, "name" : "Andy", "rating" : 10, "score" : 10 }> db.peoples.update(... { _id: 1 },... {... $set: { rating: 18 }... }... );WriteResult({ "nMatched" : 1, "nUpserted" : 0 })> db.peoples.find();{ "_id" : 1, "name" : "Andy", "rating" : 18, "score" : 10 }
2. php $ set operation
$ Where = array ("_ id" => 1); $ param = array ('$ set' => array ("score" => "100 ")); // note that the set here must be single quotes $ collection-> update ($ where, $ param); print_r ($ collection-> findOne ());
3. If $ inc does not correspond to a specific segment, it will be assigned a value directly. If yes, this value is added to the original value.
1. operations under the command line
> Db. les. find (); {"_ id": 1, "name": "Andy", "rating": 28, "score": 10}> db. les. update (... {_ id: 1 },... {... $ inc: {age: 30 }...}...); writeResult ({"nMatched": 1, "nUpserted": 0})> db. les. find (); {"_ id": 1, "age": 30, "name": "Andy", "rating": 28, "score ": 10} // for the first time, a field> db is added. les. update (... {_ id: 1 },... {... $ inc: {age: 30 }...}...); writeResult ({"nMatched": 1, "nUpserted": 0})> db. les. find (); {"_ id": 1, "age": 60, "name": "Andy", "rating": 28, "score ": 10} // The second time. If this field is found, the value is added.
2. php $ inc operation
$where = array("_id"=>1);$param = array('$inc'=>array("age"=>30));$collection->update($where,$param);print_r($collection->findOne());
4. $ unset delete a field
1. operations under the command line
> Db. les. find () ;{" _ id ": 1," age ": 120," name ":" Andy "," rating ": 28," score ": 10}> db. les. update (... {_ id: 1 },... {... $ unset: {age: ""} // Delete the age field ...}...); writeResult ({"nMatched": 1, "nUpserted": 0})> db. les. find (); {"_ id": 1, "name": "Andy", "rating": 28, "score": 10}>
2. php $ unset operation
$where = array("_id"=>1);$param = array('$unset'=>array("score"=>""));$collection->update($where,$param);print_r($collection->findOne());
V. Change Field name by $ rename
1. operations under the command line
> Db. les. find (); {"_ id": 1, "name": "Andy", "rating": 28}> db. les. update (... {_ id: 1 },... {$ rename: {"name": "firstname" }}// change name to firstname ...); writeResult ({"nMatched": 1, "nUpserted": 0})> db. les. find (); {"_ id": 1, "firstname": "Andy", "rating": 28}
2. php $ rename operation
$ Where = array ("_ id" => 1); $ param = array ('$ rename' => array ("firstname" => "name ")); // change firstname to name $ collection-> update ($ where, $ param); print_r ($ collection-> findOne ());
6. Update multiple data entries by multi
1. operations under the command line
> Db. les. find (); {"_ id": 1, "name": "Andy", "rating": 28} {"_ id": 2, "name ": "zhang", "rating": 3, "score": 5} {"_ id": 3, "name": "hao", "rating": 30, "score": 5}> db. les. update (... {rating: {$ gt: 15 }},... {$ inc: {score: 10 }},... {multi: true }...); writeResult ({"nMatched": 2, "nUpserted": 0})> db. les. find (); {"_ id": 1, "name": "Andy", "rating": 28, "score ": 10} // {"_ id": 2, "name": "zhang", "rating": 3, "score ": 5} {"_ id": 3, "name": "hao", "rating": 30, "score": 15} // The data is updated.
2. php multi operation
$where = array("rating"=>array('$gt'=>10));$param = array('$set'=>array("name"=>"tank1","rating"=>50));$ismore = array("multiple" => true);$collection->update($where,$param,$ismore);
Different versions have different functions. For example, if I use 2.6.3, $ min and $ max cannot be used, "errmsg": "Invalid modifier specified $ min" will be reported ", mongodb is expected to have a stable and high version.
Original article address: mongodb update character operation. Thank you for sharing it.