Updates a single record.
Method definition
updateOne(filter: object, update: object, options?: object): Promise<MongoResult>Request parameters
Field | Type | Required | Description |
|---|---|---|---|
| Object | Yes | The filter condition. |
| Object | Yes | This document has been updated. |
| Object | No | Control options. |
The options parameter is defined as follows:
Field | Type | Required | Description |
|---|---|---|---|
| Boolean | No | Specifies whether to insert the document if no match is found. Default value: |
Examples
Find the first record in the
userscollection where thenamefield is `jerry` and update itsagefield to 10.mpserverless.db.collection('users').updateOne({ name: 'jerry' }, { $set: { age: 10 } }) .then(res => {}) .catch(console.error);Find the first record in the
userscollection where theagefield is greater than 18 and update itsnameandagefields. Other fields are retained. If no matching record is found, insert{name: "Smith", age: 22}as a new record.mpserverless.db.collection('users').updateOne({ age: { $gt: 18 } }, { $set: { name: "Smith", age: 22 } }, { upsert: true }) .then(res => {}) .catch(console.error);
该文章对您有帮助吗?