Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: remove count and findOneAndRemove, add findOneAndDelete, countDocuments, estimatedDocumentCount #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 16, 18]
node: [16, 18, 20]
mongo: [4.2, 5.0]
services:
mongodb:
Expand Down
3 changes: 2 additions & 1 deletion lib/collection/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const methods = [
'updateMany',
'updateOne',
'replaceOne',
'count',
'countDocuments',
'estimatedDocumentCount',
'distinct',
'findOneAndDelete',
'findOneAndUpdate',
Expand Down
13 changes: 10 additions & 3 deletions lib/collection/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ class NodeCollection extends Collection {
}

/**
* count(match, options)
* countDocuments(match, options)
*/
async count(match, options) {
return this.collection.count(match, options);
async countDocuments(match, options) {
return this.collection.countDocuments(match, options);
}

/**
* estimatedDocumentCount(match, options)
*/
async estimatedDocumentCount(match, options) {
return this.collection.estimatedDocumentCount(match, options);
}

/**
Expand Down
90 changes: 60 additions & 30 deletions lib/mquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1943,47 +1943,80 @@ Query.prototype._findOne = async function _findOne() {
};

/**
* Exectues the query as a count() operation.
* Executes the query as a countDocuments() operation.
*
* #### Example:
*
* query.count().where('color', 'black').exec();
* query.countDocuments().where('color', 'black').exec();
*
* query.count({ color: 'black' })
* query.countDocuments({ color: 'black' })
*
* await query.count({ color: 'black' });
* await query.countDocuments({ color: 'black' });
*
* const doc = await query.where('color', 'black').count();
* const count = await query.where('color', 'black').countDocuments();
* console.log('there are %d kittens', count);
*
* @param {Object} [criteria] mongodb selector
* @param {Object} [filter] mongodb selector
* @return {Query} this
* @see mongodb http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Count
* @api public
*/

Query.prototype.count = function(criteria) {
this.op = 'count';
Query.prototype.countDocuments = function(filter) {
this.op = 'countDocuments';
this._validate();

if (Query.canMerge(criteria)) {
this.merge(criteria);
if (Query.canMerge(filter)) {
this.merge(filter);
}

return this;
};

/**
* Executes a `countDocuments` Query
* @returns the results
*/
Query.prototype._countDocuments = async function _countDocuments() {
const conds = this._conditions,
options = this._optionsForExec();

debug('countDocuments', this._collection.collectionName, conds, options);

return this._collection.countDocuments(conds, options);
};

/**
* Executes the query as a estimatedDocumentCount() operation.
*
* #### Example:
*
* query.estimatedDocumentCount();
*
* const count = await query.estimatedDocumentCount();
* console.log('there are %d kittens', count);
*
* @return {Query} this
* @api public
*/

Query.prototype.estimatedDocumentCount = function() {
this.op = 'estimatedDocumentCount';
this._validate();

return this;
};

/**
* Executes a `count` Query
* @returns the results
*/
Query.prototype._count = async function _count() {
Query.prototype._estimatedDocumentCount = async function _estimatedDocumentCount() {
const conds = this._conditions,
options = this._optionsForExec();

debug('count', this._collection.collectionName, conds, options);
debug('estimatedDocumentCount', this._collection.collectionName, conds, options);

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

/**
Expand Down Expand Up @@ -2329,7 +2362,7 @@ Query.prototype._findOneAndUpdate = async function() {
};

/**
* Issues a mongodb [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) remove command.
* Issues a mongodb findOneAndDelete.
*
* Finds a matching document, removes it, returning the found document (if any).
*
Expand All @@ -2339,28 +2372,25 @@ Query.prototype._findOneAndUpdate = async function() {
*
* #### Examples:
*
* await A.where().findOneAndRemove(conditions, options) // executes
* A.where().findOneAndRemove(conditions, options) // return Query
* await A.where().findOneAndRemove(conditions) // executes
* A.where().findOneAndRemove(conditions) // returns Query
* await A.where().findOneAndRemove() // executes
* A.where().findOneAndRemove() // returns Query
* A.where().findOneAndDelete() // alias of .findOneAndRemove()
* await A.where().findOneAndDelete(conditions, options) // executes
* A.where().findOneAndDelete(conditions, options) // return Query
* await A.where().findOneAndDelete(conditions) // executes
* A.where().findOneAndDelete(conditions) // returns Query
* await A.where().findOneAndDelete() // executes
* A.where().findOneAndDelete() // returns Query
*
* @param {Object} [conditions]
* @param {Object} [filter]
* @param {Object} [options]
* @return {Query} this
* @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
* @api public
*/

Query.prototype.findOneAndRemove = Query.prototype.findOneAndDelete = function(conditions, options) {
this.op = 'findOneAndRemove';
Query.prototype.findOneAndDelete = function(filter, options) {
this.op = 'findOneAndDelete';
this._validate();

// apply conditions
if (Query.canMerge(conditions)) {
this.merge(conditions);
if (Query.canMerge(filter)) {
this.merge(filter);
}

// apply options
Expand All @@ -2373,7 +2403,7 @@ Query.prototype.findOneAndRemove = Query.prototype.findOneAndDelete = function(c
* Executes a `findOneAndRemove` Query
* @returns the results
*/
Query.prototype._findOneAndRemove = async function() {
Query.prototype._findOneAndDelete = async function() {
const options = this._optionsForExec();
const conds = this._conditions;

Expand Down
12 changes: 6 additions & 6 deletions lib/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ denied.distinct.tailable = true;


denied.findOneAndUpdate =
denied.findOneAndRemove = function(self) {
denied.findOneAndDelete = function(self) {
const keys = Object.keys(denied.findOneAndUpdate);
let err;

Expand All @@ -54,12 +54,12 @@ denied.findOneAndUpdate.batchSize =
denied.findOneAndUpdate.tailable = true;


denied.count = function(self) {
denied.countDocuments = function(self) {
if (self._fields && Object.keys(self._fields).length > 0) {
return 'field selection and slice';
}

const keys = Object.keys(denied.count);
const keys = Object.keys(denied.countDocuments);
let err;

keys.every(function(option) {
Expand All @@ -73,6 +73,6 @@ denied.count = function(self) {
return err;
};

denied.count.slice =
denied.count.batchSize =
denied.count.tailable = true;
denied.countDocuments.slice =
denied.countDocuments.batchSize =
denied.countDocuments.tailable = true;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"url": "git://github.com/aheckmann/mquery.git"
},
"engines": {
"node": ">=14.0.0"
"node": ">=16.0.0"
},
"dependencies": {
"debug": "4.x"
Expand All @@ -22,7 +22,7 @@
"eslint": "8.x",
"eslint-plugin-mocha-no-only": "1.1.1",
"mocha": "9.x",
"mongodb": "5.x"
"mongodb": "6.x"
},
"bugs": {
"url": "https://github.com/aheckmann/mquery/issues/new"
Expand Down
Loading
Loading