Skip to content

Commit 7ece3ec

Browse files
authored
Merge pull request #140 from mongoosejs/vkarpov15/remove-old-methods
BREAKING CHANGE: remove count and findOneAndRemove, add findOneAndDelete, countDocuments, estimatedDocumentCount
2 parents d22970e + 7deaea6 commit 7ece3ec

File tree

7 files changed

+123
-88
lines changed

7 files changed

+123
-88
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ permissions:
88
jobs:
99
test:
1010
# os is not in the matrix, because otherwise there would be way too many testing instances
11-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-24.04
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
node: [14, 16, 18]
16-
mongo: [4.2, 5.0]
15+
node: [20, 22]
16+
mongo: [7.0, 8.0]
1717
services:
1818
mongodb:
1919
image: mongo:${{ matrix.mongo }}

lib/collection/collection.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const methods = [
1010
'updateMany',
1111
'updateOne',
1212
'replaceOne',
13-
'count',
13+
'countDocuments',
14+
'estimatedDocumentCount',
1415
'distinct',
1516
'findOneAndDelete',
1617
'findOneAndUpdate',

lib/collection/node.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@ class NodeCollection extends Collection {
3131
}
3232

3333
/**
34-
* count(match, options)
34+
* countDocuments(match, options)
3535
*/
36-
async count(match, options) {
37-
return this.collection.count(match, options);
36+
async countDocuments(match, options) {
37+
return this.collection.countDocuments(match, options);
38+
}
39+
40+
/**
41+
* estimatedDocumentCount(match, options)
42+
*/
43+
async estimatedDocumentCount(match, options) {
44+
return this.collection.estimatedDocumentCount(match, options);
3845
}
3946

4047
/**

lib/mquery.js

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,45 +1937,75 @@ Query.prototype._findOne = async function _findOne() {
19371937
};
19381938

19391939
/**
1940-
* Exectues the query as a count() operation.
1940+
* Executes the query as a countDocuments() operation.
19411941
*
19421942
* #### Example:
19431943
*
1944-
* query.count().where('color', 'black').exec();
1944+
* query.countDocuments().where('color', 'black').exec();
19451945
*
1946-
* query.count({ color: 'black' })
1946+
* query.countDocuments({ color: 'black' })
19471947
*
1948-
* await query.count({ color: 'black' });
1948+
* await query.countDocuments({ color: 'black' });
19491949
*
1950-
* const doc = await query.where('color', 'black').count();
1950+
* const count = await query.where('color', 'black').countDocuments();
19511951
* console.log('there are %d kittens', count);
19521952
*
1953-
* @param {Object} [criteria] mongodb selector
1953+
* @param {Object} [filter] mongodb selector
19541954
* @return {Query} this
1955-
* @see mongodb http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Count
19561955
* @api public
19571956
*/
19581957

1959-
Query.prototype.count = function(criteria) {
1960-
this.op = 'count';
1958+
Query.prototype.countDocuments = function(filter) {
1959+
this.op = 'countDocuments';
19611960
this._validate();
19621961

1963-
if (Query.canMerge(criteria)) {
1964-
this.merge(criteria);
1962+
if (Query.canMerge(filter)) {
1963+
this.merge(filter);
19651964
}
19661965

19671966
return this;
19681967
};
19691968

19701969
/**
1971-
* Executes a `count` Query
1970+
* Executes a `countDocuments` Query
19721971
* @returns the results
19731972
*/
1974-
Query.prototype._count = async function _count() {
1975-
const conds = this._conditions,
1976-
options = this._optionsForExec();
1973+
Query.prototype._countDocuments = async function _countDocuments() {
1974+
const conds = this._conditions;
1975+
const options = this._optionsForExec();
19771976

1978-
return this._collection.count(conds, options);
1977+
return this._collection.countDocuments(conds, options);
1978+
};
1979+
1980+
/**
1981+
* Executes the query as a estimatedDocumentCount() operation.
1982+
*
1983+
* #### Example:
1984+
*
1985+
* query.estimatedDocumentCount();
1986+
*
1987+
* const count = await query.estimatedDocumentCount();
1988+
* console.log('there are %d kittens', count);
1989+
*
1990+
* @return {Query} this
1991+
* @api public
1992+
*/
1993+
1994+
Query.prototype.estimatedDocumentCount = function() {
1995+
this.op = 'estimatedDocumentCount';
1996+
this._validate();
1997+
1998+
return this;
1999+
};
2000+
2001+
/**
2002+
* Executes a `count` Query
2003+
* @returns the results
2004+
*/
2005+
Query.prototype._estimatedDocumentCount = async function _estimatedDocumentCount() {
2006+
const conds = this._conditions;
2007+
const options = this._optionsForExec();
2008+
return this._collection.estimatedDocumentCount(conds, options);
19792009
};
19802010

19812011
/**
@@ -2313,7 +2343,7 @@ Query.prototype._findOneAndUpdate = async function() {
23132343
};
23142344

23152345
/**
2316-
* Issues a mongodb [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) remove command.
2346+
* Issues a mongodb findOneAndDelete.
23172347
*
23182348
* Finds a matching document, removes it, returning the found document (if any).
23192349
*
@@ -2323,28 +2353,25 @@ Query.prototype._findOneAndUpdate = async function() {
23232353
*
23242354
* #### Examples:
23252355
*
2326-
* await A.where().findOneAndRemove(conditions, options) // executes
2327-
* A.where().findOneAndRemove(conditions, options) // return Query
2328-
* await A.where().findOneAndRemove(conditions) // executes
2329-
* A.where().findOneAndRemove(conditions) // returns Query
2330-
* await A.where().findOneAndRemove() // executes
2331-
* A.where().findOneAndRemove() // returns Query
2332-
* A.where().findOneAndDelete() // alias of .findOneAndRemove()
2356+
* await A.where().findOneAndDelete(conditions, options) // executes
2357+
* A.where().findOneAndDelete(conditions, options) // return Query
2358+
* await A.where().findOneAndDelete(conditions) // executes
2359+
* A.where().findOneAndDelete(conditions) // returns Query
2360+
* await A.where().findOneAndDelete() // executes
2361+
* A.where().findOneAndDelete() // returns Query
23332362
*
2334-
* @param {Object} [conditions]
2363+
* @param {Object} [filter]
23352364
* @param {Object} [options]
23362365
* @return {Query} this
2337-
* @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
23382366
* @api public
23392367
*/
23402368

2341-
Query.prototype.findOneAndRemove = Query.prototype.findOneAndDelete = function(conditions, options) {
2342-
this.op = 'findOneAndRemove';
2369+
Query.prototype.findOneAndDelete = function(filter, options) {
2370+
this.op = 'findOneAndDelete';
23432371
this._validate();
23442372

2345-
// apply conditions
2346-
if (Query.canMerge(conditions)) {
2347-
this.merge(conditions);
2373+
if (Query.canMerge(filter)) {
2374+
this.merge(filter);
23482375
}
23492376

23502377
// apply options
@@ -2357,7 +2384,7 @@ Query.prototype.findOneAndRemove = Query.prototype.findOneAndDelete = function(c
23572384
* Executes a `findOneAndRemove` Query
23582385
* @returns the results
23592386
*/
2360-
Query.prototype._findOneAndRemove = async function() {
2387+
Query.prototype._findOneAndDelete = async function() {
23612388
const options = this._optionsForExec();
23622389
const conds = this._conditions;
23632390

lib/permissions.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ denied.distinct.tailable = true;
3434

3535

3636
denied.findOneAndUpdate =
37-
denied.findOneAndRemove = function(self) {
37+
denied.findOneAndDelete = function(self) {
3838
const keys = Object.keys(denied.findOneAndUpdate);
3939
let err;
4040

@@ -54,12 +54,12 @@ denied.findOneAndUpdate.batchSize =
5454
denied.findOneAndUpdate.tailable = true;
5555

5656

57-
denied.count = function(self) {
57+
denied.countDocuments = function(self) {
5858
if (self._fields && Object.keys(self._fields).length > 0) {
5959
return 'field selection and slice';
6060
}
6161

62-
const keys = Object.keys(denied.count);
62+
const keys = Object.keys(denied.countDocuments);
6363
let err;
6464

6565
keys.every(function(option) {
@@ -73,6 +73,6 @@ denied.count = function(self) {
7373
return err;
7474
};
7575

76-
denied.count.slice =
77-
denied.count.batchSize =
78-
denied.count.tailable = true;
76+
denied.countDocuments.slice =
77+
denied.countDocuments.batchSize =
78+
denied.countDocuments.tailable = true;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"url": "git://github.com/aheckmann/mquery.git"
1414
},
1515
"engines": {
16-
"node": ">=14.0.0"
16+
"node": ">=16.0.0"
1717
},
1818
"devDependencies": {
1919
"eslint": "8.x",

0 commit comments

Comments
 (0)