Skip to content

Commit 8ed95c6

Browse files
committed
BREAKING CHANGE: remove count and findOneAndRemove, add findOneAndDelete, countDocuments, estimatedDocumentCount
1 parent e7a8a30 commit 8ed95c6

File tree

6 files changed

+138
-100
lines changed

6 files changed

+138
-100
lines changed

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: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,47 +1943,80 @@ Query.prototype._findOne = async function _findOne() {
19431943
};
19441944

19451945
/**
1946-
* Exectues the query as a count() operation.
1946+
* Executes the query as a countDocuments() operation.
19471947
*
19481948
* #### Example:
19491949
*
1950-
* query.count().where('color', 'black').exec();
1950+
* query.countDocuments().where('color', 'black').exec();
19511951
*
1952-
* query.count({ color: 'black' })
1952+
* query.countDocuments({ color: 'black' })
19531953
*
1954-
* await query.count({ color: 'black' });
1954+
* await query.countDocuments({ color: 'black' });
19551955
*
1956-
* const doc = await query.where('color', 'black').count();
1956+
* const count = await query.where('color', 'black').countDocuments();
19571957
* console.log('there are %d kittens', count);
19581958
*
1959-
* @param {Object} [criteria] mongodb selector
1959+
* @param {Object} [filter] mongodb selector
19601960
* @return {Query} this
1961-
* @see mongodb http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Count
19621961
* @api public
19631962
*/
19641963

1965-
Query.prototype.count = function(criteria) {
1966-
this.op = 'count';
1964+
Query.prototype.countDocuments = function(filter) {
1965+
this.op = 'countDocuments';
19671966
this._validate();
19681967

1969-
if (Query.canMerge(criteria)) {
1970-
this.merge(criteria);
1968+
if (Query.canMerge(filter)) {
1969+
this.merge(filter);
19711970
}
19721971

19731972
return this;
19741973
};
19751974

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

1984-
debug('count', this._collection.collectionName, conds, options);
2017+
debug('estimatedDocumentCount', this._collection.collectionName, conds, options);
19852018

1986-
return this._collection.count(conds, options);
2019+
return this._collection.estimatedDocumentCount(conds, options);
19872020
};
19882021

19892022
/**
@@ -2329,7 +2362,7 @@ Query.prototype._findOneAndUpdate = async function() {
23292362
};
23302363

23312364
/**
2332-
* Issues a mongodb [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) remove command.
2365+
* Issues a mongodb findOneAndDelete.
23332366
*
23342367
* Finds a matching document, removes it, returning the found document (if any).
23352368
*
@@ -2339,28 +2372,25 @@ Query.prototype._findOneAndUpdate = async function() {
23392372
*
23402373
* #### Examples:
23412374
*
2342-
* await A.where().findOneAndRemove(conditions, options) // executes
2343-
* A.where().findOneAndRemove(conditions, options) // return Query
2344-
* await A.where().findOneAndRemove(conditions) // executes
2345-
* A.where().findOneAndRemove(conditions) // returns Query
2346-
* await A.where().findOneAndRemove() // executes
2347-
* A.where().findOneAndRemove() // returns Query
2348-
* A.where().findOneAndDelete() // alias of .findOneAndRemove()
2375+
* await A.where().findOneAndDelete(conditions, options) // executes
2376+
* A.where().findOneAndDelete(conditions, options) // return Query
2377+
* await A.where().findOneAndDelete(conditions) // executes
2378+
* A.where().findOneAndDelete(conditions) // returns Query
2379+
* await A.where().findOneAndDelete() // executes
2380+
* A.where().findOneAndDelete() // returns Query
23492381
*
2350-
* @param {Object} [conditions]
2382+
* @param {Object} [filter]
23512383
* @param {Object} [options]
23522384
* @return {Query} this
2353-
* @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
23542385
* @api public
23552386
*/
23562387

2357-
Query.prototype.findOneAndRemove = Query.prototype.findOneAndDelete = function(conditions, options) {
2358-
this.op = 'findOneAndRemove';
2388+
Query.prototype.findOneAndDelete = function(filter, options) {
2389+
this.op = 'findOneAndDelete';
23592390
this._validate();
23602391

2361-
// apply conditions
2362-
if (Query.canMerge(conditions)) {
2363-
this.merge(conditions);
2392+
if (Query.canMerge(filter)) {
2393+
this.merge(filter);
23642394
}
23652395

23662396
// apply options
@@ -2373,7 +2403,7 @@ Query.prototype.findOneAndRemove = Query.prototype.findOneAndDelete = function(c
23732403
* Executes a `findOneAndRemove` Query
23742404
* @returns the results
23752405
*/
2376-
Query.prototype._findOneAndRemove = async function() {
2406+
Query.prototype._findOneAndDelete = async function() {
23772407
const options = this._optionsForExec();
23782408
const conds = this._conditions;
23792409

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
@@ -22,7 +22,7 @@
2222
"eslint": "8.x",
2323
"eslint-plugin-mocha-no-only": "1.1.1",
2424
"mocha": "9.x",
25-
"mongodb": "5.x"
25+
"mongodb": "6.x"
2626
},
2727
"bugs": {
2828
"url": "https://github.com/aheckmann/mquery/issues/new"

0 commit comments

Comments
 (0)