replaceOne
Query for and replace an entire record.
Method definition
replaceOne(filter: object, doc: object, options?: object): Promise<MongoResult>Request parameters
Field name | Type | Required | Description |
|---|---|---|---|
| Object | Yes | The filter condition for the operation. |
| Object | Yes | The replacement document. |
| Object | No | Control. |
The options parameter:
Field name | Type | Required | Description |
|---|---|---|---|
| Boolean | No | Specifies whether to insert a new document if no match is found. The default value is |
Examples
Finds the first document in the
userscollection where thenamefield is tom and theagefield is 18, and replaces it with{ name: 'jerry' }. If no matching document is found, no operation is performed.mpserverless.db.collection('users').replaceOne({ name: 'tom', age: 18 }, { name: 'jerry' }) .then(res => {}) .catch(console.error);Finds the first document in the
userscollection where thenamefield is tom and theagefield is 18, and replaces it with{ name: 'jerry' }. If no matching document is found, a new document is inserted.mpserverless.db.collection('users').replaceOne({ name: 'tom', age: 18 }, { name: 'jerry' }, { upsert: true }) .then(res => {}) .catch(console.error);