Skip to content

Commit 0fd43c4

Browse files
authored
Merge pull request Automattic#15113 from Automattic/vkarpov15/Automatticgh-15107
docs(connection+document+model): remove remaining references to remove(), clarify that deleteOne() does not execute until then() or exec()
2 parents e561164 + f4a969f commit 0fd43c4

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

lib/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ Connection.prototype.withSession = async function withSession(executor) {
663663
*
664664
* const session = await conn.startSession();
665665
* let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
666-
* await doc.remove();
666+
* await doc.deleteOne();
667667
* // `doc` will always be null, even if reading from a replica set
668668
* // secondary. Without causal consistency, it is possible to
669669
* // get a doc back from the below query if the query reads from a

lib/document.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,17 +2357,17 @@ Document.prototype.$isDefault = function(path) {
23572357
};
23582358

23592359
/**
2360-
* Getter/setter, determines whether the document was removed or not.
2360+
* Getter/setter, determines whether the document was deleted. The `Model.prototype.deleteOne()` method sets `$isDeleted` if the delete operation succeeded.
23612361
*
23622362
* #### Example:
23632363
*
2364-
* const product = await product.remove();
2364+
* const product = await product.deleteOne();
23652365
* product.$isDeleted(); // true
2366-
* product.remove(); // no-op, doesn't send anything to the db
2366+
* product.deleteOne(); // no-op, doesn't send anything to the db
23672367
*
23682368
* product.$isDeleted(false);
23692369
* product.$isDeleted(); // false
2370-
* product.remove(); // will execute a remove against the db
2370+
* product.deleteOne(); // will execute a remove against the db
23712371
*
23722372
*
23732373
* @param {Boolean} [val] optional, overrides whether mongoose thinks the doc is deleted

lib/model.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,20 @@ Model.prototype.$__where = function _where(where) {
694694
};
695695

696696
/**
697-
* Delete this document from the db.
697+
* Delete this document from the db. Returns a Query instance containing a `deleteOne` operation by this document's `_id`.
698698
*
699699
* #### Example:
700700
*
701701
* await product.deleteOne();
702702
* await Product.findById(product._id); // null
703703
*
704+
* Since `deleteOne()` returns a Query, the `deleteOne()` will **not** execute unless you use either `await`, `.then()`, `.catch()`, or [`.exec()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.exec())
705+
*
706+
* #### Example:
707+
*
708+
* product.deleteOne(); // Doesn't do anything
709+
* product.deleteOne().exec(); // Deletes the document, returns a promise
710+
*
704711
* @return {Query} Query
705712
* @api public
706713
*/
@@ -1879,8 +1886,6 @@ Model.translateAliases = function translateAliases(fields, errorOnDuplicates) {
18791886
/**
18801887
* Deletes the first document that matches `conditions` from the collection.
18811888
* It returns an object with the property `deletedCount` indicating how many documents were deleted.
1882-
* Behaves like `remove()`, but deletes at most one document regardless of the
1883-
* `single` option.
18841889
*
18851890
* #### Example:
18861891
*
@@ -1914,8 +1919,6 @@ Model.deleteOne = function deleteOne(conditions, options) {
19141919
/**
19151920
* Deletes all of the documents that match `conditions` from the collection.
19161921
* It returns an object with the property `deletedCount` containing the number of documents deleted.
1917-
* Behaves like `remove()`, but deletes all documents that match `conditions`
1918-
* regardless of the `single` option.
19191922
*
19201923
* #### Example:
19211924
*
@@ -2729,7 +2732,7 @@ Model.create = async function create(doc, options) {
27292732
* // operationType: 'delete',
27302733
* // ns: { db: 'mydb', coll: 'Person' },
27312734
* // documentKey: { _id: 5a51b125c5500f5aa094c7bd } }
2732-
* await doc.remove();
2735+
* await doc.deleteOne();
27332736
*
27342737
* @param {Array} [pipeline]
27352738
* @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#watch)
@@ -2777,7 +2780,7 @@ Model.watch = function(pipeline, options) {
27772780
*
27782781
* const session = await Person.startSession();
27792782
* let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
2780-
* await doc.remove();
2783+
* await doc.deleteOne();
27812784
* // `doc` will always be null, even if reading from a replica set
27822785
* // secondary. Without causal consistency, it is possible to
27832786
* // get a doc back from the below query if the query reads from a

0 commit comments

Comments
 (0)