Monday, May 23, 2011

Updating Records in MongoDb

Few important notes to remember when updating records in MongoDb

1. Multi update only works with $ operators
2. You can use upsert to update old items and insert new items
3. Updating without a $ operator like $set, $inc replaces the entire document.
4. In the time of writing, the default is a single update even a given criteria matches more records. You must explicitly specify multi = true, multiple = true depending on the client / driver you're using.
5. It is recommended then to explicitly mention whether you want to update one or more records.
6. You can use safe or fsync to make sure that the update is successful before returning to the driver/client.


Borrowed from php.net (http://www.php.net/manual/en/mongocollection.update.php)

1. If you are using replication and the master has changed, using "safe" will make the driver disconnect from the master, throw and exception, and attempt to find a new master on the next operation (your application must decide whether or not to retry the operation on the new master).

2. If you do not use "safe" with a replica set and the master changes, there will be no way for the driver to know about the change so it will continuously and silently fail to write.

3. If safe is an integer, will replicate the update to that many machines before returning success (or throw an exception if the replication times out, see wtimeout). This overrides the w variable set on the collection.
  • Related Links Widget for Blogspot

No comments: