Database Reference
In-Depth Information
There are three parts to the update( ) function:
Database . We need to identify which database we are updating. More accurately,
the variable pointing to the database we are updating, such as in this case db .
Collection . We need to identify the collection within this database we are updating.
Parameters . We need to identify the documents within this collection we would
like changed. The first parameter contains the criteria for finding the documents we
would like changed, which is identical to the syntax in the find( ) function because
we are querying for documents. The second parameter, called “changes,” specifies
how we would like to change the documents we find. We may decide to modify the
values in one or more fields. The third parameter contains two indicators that can
appear in any order: upsert and multi . Upsert means “update else insert,” meaning
if this indicator is set to true or 1 and the criteria does not bring back any docu-
ments in the collection to update, then a new document will be created. If we leave
out the upsert parameter or set it to false or 0 , a new document will not be created.
Multi is an indicator that, if set to true or 1 , tells MongoDB to update all of the
documents that meet the criteria. If we leave out the multi parameter or set it to
false or 0 , only the first document encountered will be updated.
Search WWH ::




Custom Search