Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. To update a field that is nested in the JSON structure the following command can be used:. For numeric field values it is possible to modify the value regarding their old value. The following example will increase the zip code of a customer by one. When the zip code is it will be after the update. To do this just specify a negative value.
The following command will decrease the value by 5. Note: Even if there is more then one document that matches the query selector only the first document will be updated. To update all matching documents an addional option must be set:. In this example nMatched shows that two documents have been found by the document selector query but nModified shows that only one document have been updated by the update command.
For examples, see Update with Aggregation Pipeline. When using the upsert: true option with the update command, and not using a unique index on the query field s , multiple instances of an update operation with similar query field s could result in duplicate documents being inserted in certain circumstances.
Consider an example where no document with the name Andy exists and multiple clients issue the following command at roughly the same time:. If all update operations finish the query phase before any client successfully inserts data, and there is no unique index on the name field, each update operation may result in an insert, creating multiple documents with name: Andy.
To ensure that only one such document is created, and the other update operations update this new document instead, create a unique index on the name field. This guarantees that only one document with name: Andy is permitted in the collection. With this unique index in place, the multiple update operations now exhibit the following behavior:.
For each update element in the updates array, the sum of the query and the update sizes i. The total number of update statements in the updates array must be less than or equal to the maximum bulk size.
The update command adds support for the bypassDocumentValidation option, which lets you bypass document validation when inserting or updating documents in a collection with validation rules. To use update with multi: false on a sharded collection,. If you specify upsert: true , the filter q must include an equality match on the shard key. However, starting in version 4.
If the operation cannot target a single shard by the query filter, it then attempts to target by the replacement document. In MongoDB 4. To modify the existing shard key value with update :. See also upsert on a Sharded Collection. Starting in version 4. To use update to set the document's missing shard key, you must run on a mongos. Do not issue the operation directly on the shard. In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design.
For many scenarios, the denormalized data model embedded documents and arrays will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.
For additional transactions usage considerations such as runtime limit and oplog size limit , see also Production Considerations.
Specifically, in MongoDB 4. If run on a non-existing collection, the operation creates the collection. Create Collections and Indexes In a Transaction. Use update operators to update only the specified fields of a document. For example, create a members collection with the following documents:. The returned document shows that the command found and updated a single document.
The command returns:. Use update operators to update only the specified fields of a document, and include the multi field set to true in the update statement. For example, a members collection contains the following documents:. The update modifies all documents that match the query specified in the q field, namely the empty query which matches all documents in the collection. The returned document shows that the command found and updated multiple documents. For a replica set, the command returns:.
The following examples uses the aggregation pipeline to modify a field using the values of the other fields in the document. A members collection contains the following documents:. Assume that instead of separate misc1 and misc2 fields, you want to gather these into a new comments field. The following update operation uses an aggregation pipeline to add the new comments field and remove the misc1 and misc2 fields for all documents in the collection. The aggregation pipeline allows the update to perform conditional updates based on the current field values as well as use current field values to calculate a separate field value.
Using an aggregation pipeline, you can update the documents with the calculated grade average and letter grade. The following example performs multiple update operations on the members collection:.
See Output for details. A collection myColl has the following documents:. The following operation includes the collation option:. Starting in MongoDB 3.
Create a collection students with the following documents:. Create a collection students2 with the following documents:. Create a sample members collection with the following documents:. To see the index used, run explain on the operation:. The explain does not modify the documents.
0コメント