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

DeprecationWarning: collection.count is deprecated #144

Open
yohanesgultom opened this issue Jul 13, 2018 · 12 comments
Open

DeprecationWarning: collection.count is deprecated #144

yohanesgultom opened this issue Jul 13, 2018 · 12 comments

Comments

@yohanesgultom
Copy link

yohanesgultom commented Jul 13, 2018

I use Node v8.11.1 and these packages:

"mongoose": "^5.2.2",
"mongoose-paginate": "^5.0.3"

And this get this warning during execution of paginate():

(node:9893) DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount instead

@suprithwork
Copy link

Even I have the same error

@pararell
Copy link

pararell commented Jul 15, 2018

Same problem here and because I'm not sure if there will be any update here, I have created small service on base this library, used in schema on the same principle but return promise and using only page, sort and limit, maybe it will help -

function paginate(query, options) {
    query   = query || {};
    options = Object.assign({}, paginate.options, options);

    const sort  = options.sort;
    const limit = options.hasOwnProperty('limit') ? options.limit : 10;
    const page  = options.page || 1;
    const skip  = options.hasOwnProperty('page') ? ((page - 1) * limit) : 0;
    const docs = limit
     ? this.find(query).sort(sort).skip(skip).limit(limit).exec()
     : query.exec();
    const countDocuments = this.countDocuments(query).exec();

    return Promise.all([docs, countDocuments]).then(function(values) {
      return Promise.resolve({
        docs:  values[0],
        total: values[1],
        limit: limit,
        page: page,
        pages: Math.ceil(values[1] / limit) || 1
      })
    });
}

module.exports = function(schema) {
    schema.statics.paginate = paginate;
};

module.exports.paginate = paginate;

@c-carrasco
Copy link

Same problem. I have forked the project (from version-5.0.3) to fix the warning.

npm install git://github.com/zerok78/mongoose-paginate.git#version-5.0.4 -S

@edenmw
Copy link

edenmw commented Aug 4, 2018

My approach
\node_modules\mquery\lib\collection\node.js:50
NodeCollection.prototype.count = function(match, options, cb) {
this.collection.count(match, options, cb);
};

change :

NodeCollection.prototype.countDocuments = function(match, options, cb) {
this.collection.countDocuments(match, options, cb);
};

\node_modules\mongoose\lib\query.js:1950
this._collection.count(conds, options, utils.tick(callback));

change :

this._collection.countDocuments(conds, options, utils.tick(callback));

@alielmajdaoui
Copy link

Still the same issue!
image

@bitfede
Copy link

bitfede commented Mar 21, 2019

I see DaviRJ fixed this 4 days ago... how do I update mongoose-paginate so that this fix is included in my node backend?

doing npm update mongoose-paginate does not do anything. sorry for the ignorance and thanks for the help!

@AmrSaber
Copy link

AmrSaber commented Jun 5, 2019

@bitfede He didn't, there are 11 tests failing, so his branch is not merged.

@paulbertil
Copy link

Is there any fix to this?

@lechuit
Copy link

lechuit commented Sep 9, 2019

Is there any fix to this?

in:
node_modules/mongoose/lib/query.js

  • this._collection.count(conds, options, utils.tick(callback));

how to fix:
this._collection.countDocuments(conds, options, utils.tick(callback));

@AmrSaber
Copy link

AmrSaber commented Sep 17, 2019

@lechuit,That's not a proper solution, will need to fix it each time node modules are generated.

@paulbertil, I would suggest using mongoose-paginate-v2 instead.

@edsonnt2
Copy link

edsonnt2 commented Feb 3, 2020

Thank's @AmrSaber

@mahdipakravan-dev
Copy link

Is there any fix to this?

in:
node_modules/mongoose/lib/query.js

  • this._collection.count(conds, options, utils.tick(callback));

how to fix:
this._collection.countDocuments(conds, options, utils.tick(callback));

This is Work , But How Can We Trust This ? Because in The Server or docker container we Can Not Access To this file
i think we should tell this problem to mongoose Contributors to fix this on next version

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

No branches or pull requests