Skip to content

Commit

Permalink
Merge pull request #13618 from Automattic/IslandRhythms/gh-13598
Browse files Browse the repository at this point in the history
remove `count()`
  • Loading branch information
vkarpov15 committed Jul 18, 2023
2 parents eeefdd2 + 363f2aa commit 1a69e7c
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 110 deletions.
1 change: 0 additions & 1 deletion lib/helpers/query/validOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module.exports = Object.freeze([
// Read
'count',
'countDocuments',
'distinct',
'estimatedDocumentCount',
Expand Down
28 changes: 0 additions & 28 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2235,34 +2235,6 @@ Model.countDocuments = function countDocuments(conditions, options) {
return mq.countDocuments(conditions);
};

/**
* Counts number of documents that match `filter` in a database collection.
*
* This method is deprecated. If you want to count the number of documents in
* a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](https://mongoosejs.com/docs/api/model.html#Model.estimatedDocumentCount())
* instead. Otherwise, use the [`countDocuments()`](https://mongoosejs.com/docs/api/model.html#Model.countDocuments()) function instead.
*
* #### Example:
*
* const count = await Adventure.count({ type: 'jungle' });
* console.log('there are %d jungle adventures', count);
*
* @deprecated
* @param {Object} [filter]
* @return {Query}
* @api public
*/

Model.count = function count(conditions) {
_checkContext(this, 'count');
if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {
throw new MongooseError('Model.count() no longer accepts a callback');
}

const mq = new this.Query({}, {}, this, this.$__collection);

return mq.count(conditions);
};

/**
* Creates a Query for a `distinct` operation.
Expand Down
74 changes: 0 additions & 74 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2584,35 +2584,6 @@ Query.prototype.findOne = function(conditions, projection, options) {
return this;
};

/**
* Execute a count query
*
* @see count https://www.mongodb.com/docs/manual/reference/method/db.collection.count/
* @api private
*/

Query.prototype._count = async function _count() {
try {
this.cast(this.model);
} catch (err) {
this.error(err);
}

if (this.error()) {
throw this.error();
}

applyGlobalMaxTimeMS(this.options, this.model);
applyGlobalDiskUse(this.options, this.model);

const options = this._optionsForExec();

this._applyTranslateAliases(options);

const conds = this._conditions;

return this._collection.collection.count(conds, options);
};

/**
* Execute a countDocuments query
Expand Down Expand Up @@ -2689,51 +2660,6 @@ Query.prototype._estimatedDocumentCount = async function _estimatedDocumentCount
return this._collection.collection.estimatedDocumentCount(options);
};

/**
* Specifies this query as a `count` query.
*
* This method is deprecated. If you want to count the number of documents in
* a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](https://mongoosejs.com/docs/api/query.html#Query.prototype.estimatedDocumentCount())
* instead. Otherwise, use the [`countDocuments()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.countDocuments()) function instead.
*
* This function triggers the following middleware.
*
* - `count()`
*
* #### Example:
*
* const countQuery = model.where({ 'color': 'black' }).count();
*
* query.count({ color: 'black' }).count().exec();
*
* await query.count({ color: 'black' });
*
* query.where('color', 'black').count();
*
* @deprecated
* @param {Object} [filter] count documents that match this object
* @return {Query} this
* @see count https://www.mongodb.com/docs/manual/reference/method/db.collection.count/
* @api public
*/

Query.prototype.count = function(filter) {
if (typeof filter === 'function' ||
typeof arguments[1] === 'function') {
throw new MongooseError('Query.prototype.count() no longer accepts a callback');
}

this.op = 'count';
this._validateOp();

filter = utils.toObject(filter);

if (mquery.canMerge(filter)) {
this.merge(filter);
}

return this;
};

/**
* Specifies this query as a `estimatedDocumentCount()` query. Faster than
Expand Down
3 changes: 1 addition & 2 deletions test/model.middleware.preposttypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('pre/post hooks, type of this', function() {
const MongooseDocumentMiddleware = [...MongooseDistinctDocumentMiddleware, ...MongooseQueryAndDocumentMiddleware];

const MongooseDistinctQueryMiddleware = [
'count', 'estimatedDocumentCount', 'countDocuments',
'estimatedDocumentCount', 'countDocuments',
'deleteMany', 'distinct',
'find', 'findOne', 'findOneAndDelete', 'findOneAndRemove', 'findOneAndReplace', 'findOneAndUpdate',
'replaceOne', 'updateMany'];
Expand Down Expand Up @@ -278,7 +278,6 @@ describe('pre/post hooks, type of this', function() {
await doc.save(); // triggers save and validate hooks

// MongooseDistinctQueryMiddleware
await Doc.count().exec();
await Doc.estimatedDocumentCount().exec();
await Doc.countDocuments().exec();
await Doc.deleteMany().exec(); await Doc.create({ data: 'value' });
Expand Down
8 changes: 4 additions & 4 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2319,7 +2319,7 @@ describe('Model', function() {
const title = 'interop ad-hoc as promise';

const created = await BlogPost.create({ title: title });
const query = BlogPost.count({ title: title });
const query = BlogPost.countDocuments({ title: title });
const found = await query.exec('findOne');
assert.equal(found.id, created.id);
});
Expand Down Expand Up @@ -5304,7 +5304,7 @@ describe('Model', function() {
}
]);

const beforeExpirationCount = await Test.count({});
const beforeExpirationCount = await Test.countDocuments({});
assert.ok(beforeExpirationCount === 12);

let intervalid;
Expand All @@ -5318,7 +5318,7 @@ describe('Model', function() {
// in case it happens faster, to reduce test time
new Promise(resolve => {
intervalid = setInterval(async() => {
const count = await Test.count({});
const count = await Test.countDocuments({});
if (count === 0) {
resolve();
}
Expand All @@ -5328,7 +5328,7 @@ describe('Model', function() {

clearInterval(intervalid);

const afterExpirationCount = await Test.count({});
const afterExpirationCount = await Test.countDocuments({});
assert.equal(afterExpirationCount, 0);
});

Expand Down
1 change: 0 additions & 1 deletion test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,6 @@ describe('Query', function() {
const TestSchema = new Schema({ name: String });

const ops = [
'count',
'find',
'findOne',
'findOneAndRemove',
Expand Down

0 comments on commit 1a69e7c

Please sign in to comment.