MONGO database update operation has a key feature relative to MySQL, it can use Upsert mode, when the updated data does not exist, directly inserted, but thinkphp MONGO driver actually does not support this feature, no way, self-reliance.
thinkphp Driver layer, see because of the support of a variety of Db, but also using inheritance, the use of the class hierarchy is deep, responsible for the MONGO driver is DbMongo.class.php, the file is located in Thinkphp\extend\driver\db, Modifies the $options parameter of the Update method and, if the Update method provides the Upsert option, updates MongoDB in Upsert mode.
1: Public function Update ($data, $options) {
2: if (isset ($options [' table '])) {
3: $this->switchcollection ($options [' table ']);
4: }
5: $This->model = $options [' model '];
6: N (' Db_write ', 1);
7: $query = $this->parsewhere ($options [' where ']);
8: $set = $this->parseset ($data);
9: $isUpsert = $options [false;
Ten: if ($this->debug) {
One: $this->querystr = $this->_dbname.'. '. $this->_collectionname. '. Update (';
: $this->querystr . = $query? Json_encode ($query):' {} ';
: $this->querystr . = ', '. Json_encode ($set). ';
: }
: try{
: //record start execution time
: G (' querystarttime ');
: $result = $this->_collection->update ($query, $set, Array (true));
: $this->debug ();
: return $result;
: catch (mongocursorexception $e) {
: throw_exception ($e->getmessage ());
: }
: }
thinkphp MONGO driver Update method supports Upsert parameters