MongoDB Update character manipulation
Zhang Ying published in 2014-07-23
Category list: NoSQL
Tags: $inc, $rename, $set, $unset, MongoDB, Multi, update, Upsert
The following commonly used update operation, with the mongodb2.6.3 version of the test, the official release of stable version 2.4, the proposed stable version.
One, upsert means that if there is data is not inserted, no data inserted
1, under command line
View copy print?
- > db.peoples.update ( //Find users with name equals tank
- ... {name: "Tank"},
- ... {
- ... "_id": 1,
- ... name: "Andy",
- ... rating:10,
- ... score:10
- ... },
- ... {Upsert:true} //If not, insert
- ... );
- Writeresult ({ "nmatched": 0, "nupserted": 1, "_id": 1})
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating": Ten, "Score": Ten}
- > Db.peoples.update (
- ... {_id:1},
- ... {
- ... "_id": 1,
- ... name: "Andy",
- ... rating:10,
- ... score:10
- ... },
- ... {Upsert:true}
- ... );
- Writeresult ({ "nmatched": 1, "nupserted": 0}) //No insert operation with matching data
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating": Ten, "Score": Ten}
2,php Upsert operation
View copy print?
- $collection->update (
- Array ("name" = " Zhang"),
- Array ("_id" =>2,"name" = "Tank","rating" =>10,"Score" =>10),
- Array ("upsert" = True)
- );
- Print_r ($collection->findone ());
Second, $set replacement value
1, command-line operation
View copy print?
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating": Ten, "Score": Ten}
- > Db.peoples.update (
- ... {_id:1},
- ... {
- ... $set: {rating:18}
- ... }
- ... );
- Writeresult ({ "nmatched": 1, "nupserted": 0})
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating":, "Score": Ten}
2,php $set operation
View copy print?
- $where = Array ("_id" =>1);
- $param = Array (' $set ' = = =Array ("score" = "100")); //Note that the set here must be a single quote
- $collection->update ($where,$param);
- Print_r ($collection->findone ());
Third, $inc if there is no direct assignment to the response paragraph, if there is a value added to the original value
1, command-line operation
View copy print?
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating": +, "score": Ten}
- > Db.peoples.update (
- ... {_id:1},
- ... {
- ... $inc: {age:30}
- ... }
- ... );
- Writeresult ({ "nmatched": 1, "nupserted": 0})
- > Db.peoples.find ();
- { "_id": 1, "age": +, "name": "Andy", "rating": " Score": ten} //First time, add a field
- > Db.peoples.update (
- ... {_id:1},
- ... {
- ... $inc: {age:30}
- ... }
- ... );
- Writeresult ({ "nmatched": 1, "nupserted": 0})
- > Db.peoples.find ();
- { "_id": 1, "age": $, " name": "Andy", "rating": " Score": ten} //The second time, found this field to add the value of the
2,php $inc operation
View copy print?
- $where = Array ("_id" =>1);
- $param = Array (' $inc ' = =Array ("age" =>30));
- $collection->update ($where,$param);
- Print_r ($collection->findone ());
Four, $unset delete fields
1, command-line operation
View copy print?
- > Db.peoples.find ();
- { "_id": 1, "age": +, "name": "Andy", "rating":, "Score": Ten}
- > Db.peoples.update (
- ... {_id:1},
- ... {
- ... $unset: {Age: "} //delete age field
- ... }
- ... );
- Writeresult ({ "nmatched": 1, "nupserted": 0})
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating": +, "score": Ten}
- >
2,php $unset operation
View copy print?
- $where = Array ("_id" =>1);
- $param = Array (' $unset ' = = =Array ("score" and "="));
- $collection->update ($where,$param);
- Print_r ($collection->findone ());
Five, $rename modify field names
1, command-line operation
View copy print?
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating":
- > Db.peoples.update (
- ... {_id:1},
- ... { $rename: { "name": "FirstName"}} //change name to FirstName
- ... );
- Writeresult ({ "nmatched": 1, "nupserted": 0})
- > Db.peoples.find ();
- { "_id": 1, "FirstName": "Andy", "rating":
2,php $rename operation
View copy print?
- $where = Array ("_id" =>1);
- $param = Array (' $rename ' = = =Array ("FirstName" = "name")); //change FirstName to name
- $collection->update ($where,$param);
- Print_r ($collection->findone ());
Six, multi update more than one data
1, command-line operation
View copy print?
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating":
- { "_id": 2, "name": "Zhang", "rating": 3, "Score": 5}
- { "_id": 3, "name": "Hao", "rating": +, "score": 5}
- > Db.peoples.update (
- ... {rating: { $gt:}},
- ... { $inc: {score:10}},
- ... {Multi:true}
- ... );
- Writeresult ({ "nmatched": 2, "nupserted": 0})
- > Db.peoples.find ();
- { "_id": 1, "name": "Andy", "rating": " Score": ten} //This data updated
- { "_id": 2, "name": "Zhang", "rating": 3, "Score": 5}
- { "_id": 3, "name": "Hao", "rating": +, "score": [] //This data updated
2,php Multi Operation
View copy print?
- $where = array ( Span class= "string" > "rating" =>array ( ' $gt ' =>10));
- $param = array ( ' $set ' =>array ( "name" =
- $ismore = array ( Span class= "string", "multiple" => true);
- $collection->update ( $where, $param, $ismore)
Version is different, the function will be different, for example, I use the version is 2.6.3, $min and $max can not use, will be reported "errmsg": "Invalid modifier specified $min", Hope MongoDB out a stable high version.
Reprint Please specify
Author: submarine Eagle
Address: http://blog.51yip.com/nosql/1638.html
MongoDB Update character manipulation