Skip to content
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ permissions:
jobs:
test:
# os is not in the matrix, because otherwise there would be way too many testing instances
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
node: [14, 16, 18]
mongo: [4.2, 5.0]
node: [20, 22]
mongo: [7.0, 8.0]
services:
mongodb:
image: mongo:${{ matrix.mongo }}
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
91 changes: 59 additions & 32 deletions lib/mquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1937,45 +1937,75 @@ 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 `count` Query
* Executes a `countDocuments` Query
* @returns the results
*/
Query.prototype._count = async function _count() {
const conds = this._conditions,
options = this._optionsForExec();
Query.prototype._countDocuments = async function _countDocuments() {
const conds = this._conditions;
const options = this._optionsForExec();

return this._collection.count(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._estimatedDocumentCount = async function _estimatedDocumentCount() {
const conds = this._conditions;
const options = this._optionsForExec();
return this._collection.estimatedDocumentCount(conds, options);
};

/**
Expand Down Expand Up @@ -2313,7 +2343,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 @@ -2323,28 +2353,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 @@ -2357,7 +2384,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;
2 changes: 1 addition & 1 deletion 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"
},
"devDependencies": {
"eslint": "8.x",
Expand Down
Loading
Loading