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

MongoDB < 3.4: Fix for countDocument #215

Merged
merged 1 commit into from
May 22, 2024

Conversation

brianpham93
Copy link
Contributor

@brianpham93 brianpham93 commented May 20, 2024

MongoDB < 3.4 is only supported by mongoose 5 or below
In mongoose 5, Model.countDocument's signature is Model.countDocuments = function countDocuments(conditions, callback) {...}.

/**
 * Counts number of documents matching `filter` in a database collection.
 *
 * ####Example:
 *
 *     Adventure.countDocuments({ type: 'jungle' }, function (err, count) {
 *       console.log('there are %d jungle adventures', count);
 *     });
 *
 * If you want to count all documents in a large collection,
 * use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model.estimatedDocumentCount)
 * instead. If you call `countDocuments({})`, MongoDB will always execute
 * a full collection scan and **not** use any indexes.
 *
 * The `countDocuments()` function is similar to `count()`, but there are a
 * [few operators that `countDocuments()` does not support](https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments).
 * Below are the operators that `count()` supports but `countDocuments()` does not,
 * and the suggested replacement:
 *
 * - `$where`: [`$expr`](https://docs.mongodb.com/manual/reference/operator/query/expr/)
 * - `$near`: [`$geoWithin`](https://docs.mongodb.com/manual/reference/operator/query/geoWithin/) with [`$center`](https://docs.mongodb.com/manual/reference/operator/query/center/#op._S_center)
 * - `$nearSphere`: [`$geoWithin`](https://docs.mongodb.com/manual/reference/operator/query/geoWithin/) with [`$centerSphere`](https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere)
 *
 * @param {Object} filter
 * @param {Function} [callback]
 * @return {Query}
 * @api public
 */

Model.countDocuments = function countDocuments(conditions, callback) {
  _checkContext(this, 'countDocuments');

  if (typeof conditions === 'function') {
    callback = conditions;
    conditions = {};
  }

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

  callback = this.$handleCallbackError(callback);

  return mq.countDocuments(conditions, callback);
};

Hence, passing findOptions as callback resulted in an error later saying expected parameter must be callback, not an object

@aravindnc
Copy link
Owner

findOption is passed for another fix, removing it will break it. So may be we need to set this is config or so.

@brianpham93
Copy link
Contributor Author

what was that fix for ? Model.countDocuments in mongoose 5 and below doesn't accept findOptions , and it's the last version that support MongoDB 3.4 so i think it should be fine removing it

https://mongoosejs.com/docs/compatibility.html

@aravindnc
Copy link
Owner

This is the issue. #186

@aravindnc aravindnc merged commit 64a7640 into aravindnc:main May 22, 2024
1 of 2 checks passed
@aravindnc
Copy link
Owner

@brianpham93 Merged! Thanks for pointing out.

@brianpham93 brianpham93 deleted the fix-for-mongoose5 branch May 22, 2024 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants