Database Reference
In-Depth Information
Updating Your Data
The way you use Python's update() function doesn't vary much from how you use the identically named function in
the MongoDB shell or the PHP driver. In this case, you provide two mandatory parameters to update your data: arg
and doc . The arg parameter specifies the key/value information used to match a document, and the doc parameter
contains the updated information. You can also provide several optional parameter to specify your options. The following
list covers Python's list of options to update information, including what they do:
upsert (optional) : If set to True , performs an upsert.
manipulate (optional) : If set to True , indicates the document will be manipulated before
performing the update using all instances of the SONManipulator . For more information, refer
to the son_manipulator documentation ( http://api.mongodb.org/python/current/api/
pymongo/son_manipulator.html ) .
check_keys (optional) : If set to True , update() will check whether any of the keys in the
document start with the restricted characters '$' or .' when replacing arg .
multi (optional) : If set to True , updates any matching document, rather than just the first
document it finds (the default action). It is recommended that you always set this to True or
False , rather than relying on the default behavior (which could always change in the future).
w (optional) : If set to 0, the update operation will not be acknowledged. When working with
replica sets, w can also be set to n , ensuring that the primary server acknowledges the update
operation when successfully replicated to n nodes. Can also be set to majority —a reserved
string—to ensure that the majority of replica nodes will acknowledge the update, or to a
specific tag, ensuring that the nodes tagged will acknowledge the update. This option defaults
to 1, acknowledging the update operation.
wtimeout (optional) : Used to specify how long the server is to wait for receiving
acknowledgement (in milliseconds). Defaults to 10000.
j (optional) : If set to True , this Boolean option will force the data to be written to the journal
before indicating that the update was a success. Defaults to False .
fsync (optional ): If set to True , this Boolean option causes the data to be synced to disk
before returning a success. If this option is set to True , then it's implied that w is set to 0 ,
even if it's set otherwise.
If you do not specify any of the modifier operators when updating a document, then by default all information
in the document will be replaced with whatever data you inserted in the doc parameter. It is best to avoid relying on
the default behavior; instead, you should use the aforementioned operators to specify your desired updates explicitly
(you'll learn how to do this momentarily).
You can see why it's best to use conditional operators with the update() command by looking at a case that
doesn't use any conditional operators:
// Define the updated data
>>> update = ( {
"Type" : "Chair",
"Status" : "In use",
"Tags" : ["Chair","In use","Marketing"],
"ItemNumber" : "6789SID",
"Location" : {
"Department" : "Marketing",
"Building" : "2B",
 
Search WWH ::




Custom Search