Hướng dẫn updateone mongodb
Docs Home → MongoDB Manual Show db.collection.updateOne(filter, update, options)
Importantmongosh MethodThis page documents a For MongoDB API drivers, refer to the language-specific MongoDB driver documentation. Updates a single document within the collection based on the filter. The
The
You can include the same identifier multiple times in the update document; however, for each distinct identifier (
However, you can specify compound conditions on the same identifier in a single filter document, such as in the following examples:
For examples, see Specify hint Document or string Optional. A document or string that specifies the index to use to support the query predicate. The option can take an index specification document or the index name string. If you specify an index that does not exist, the operation errors. For an example, see Specify New in version 4.2.1. The method returns a document that contains:
On deployments running with
The built-in role
For the
update specifications, the For example:
Starting in MongoDB 4.2, the
Using the aggregation pipeline allows for a more expressive update statement, such as expressing conditional updates based on current field values or updating one field using the value of another field(s). For example:
NoteThe For examples, see Update with Aggregation Pipeline. If If you specify If an update operation changes the document size, the operation will fail. To use
However, starting in version 4.4, documents in a sharded collection can be
missing the shard key fields. To target a document that is missing the shard key, you can use the
Starting in MongoDB 4.2, you can update a document's shard key value unless the shard key field is the
immutable WarningStarting in version 4.4, documents in sharded collections can be missing the shard key fields. Take precaution to avoid accidentally removing the shard key when changing a document's shard key value. To modify the existing shard key value with
See also
Starting in version 4.4, documents in a sharded collection can be missing the shard key fields. To use
In addition, the following requirements also apply:
TipSince a missing key value is returned as part of a null equality match, to avoid updating a null-valued key, include additional query conditions (such as on the See also:
ImportantIn 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. Starting in MongoDB 4.4, you can create collections and indexes inside a multi-document transaction if the transaction is not a cross-shard write transaction. Specifically, in MongoDB 4.4 and greater, In MongoDB 4.2 and earlier, the operation must be run on an existing collection. TipSee also:Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern. The
The following operation updates a single document where
The operation returns:
If no matches were found, the operation instead returns:
Setting Starting in MongoDB 4.2, the
Using the aggregation pipeline allows for a more expressive update statement, such as expressing conditional updates based on current field values or updating one field using the value of another field(s). The following examples uses the aggregation pipeline to modify a field using the values of the other fields in the document. Create a
Assume that instead of separate
NoteThe The
$unset stage removes the misc1 and misc2 fields.After the command, the collection contains the following documents:
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. For
example, create a
The third document
NoteThe The
$set stage calculates a new field grade based on the average field calculated in the previous stage. See
$switch for more information on the $switch aggregation operator.After the command, the collection contains the following documents:
TipSee also:The
The following operation attempts to update the document with
Since
The collection now contains the following documents:
The The following operation
updates the first document with
The operation returns:
The collection now contains the following documents:
Since no documents matched the filter, and Given a three member replica set, the following operation specifies a
If the primary and at least one secondary acknowledge each write operation within 100 milliseconds, it returns:
If the acknowledgement takes longer than the Changed in version 4.4.
The following table explains the possible values of
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. A collection
The following operation includes the collation option:
Starting in MongoDB 3.6, when updating an array field, you can specify Create a collection
To modify all elements that are greater than or equal to
The operation updates the
Create a collection
To modify the value of the
The operation updates the array of a single document, and after the operation, the collection has the following documents:
New in version 4.2.1. Create a sample
Create the following indexes on the collection:
The following update operation explicitly hints to use the index NoteIf you specify an index that does not exist, the operation errors.
The update command returns the following:
To view the indexes used, you can use the
TipSee also: |