关于使用mongoose-paginate库最近出现的问题
发布于 6 年前 作者 wangjunwangjunwang 5393 次浏览 来自 分享

1. 背景 最近在mongoose-paginate库时控制台打印出一个警告: 2018-07-27 16:46:55 - - - (node:14676) DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount instead 1. 原因 在mongoose的5.2版本之后,mongoose的collection.count方法已经废弃了,官方给出的建议是使用collection.countDocuments or collection.estimatedDocumentCount两个均可,mongoose-paginate库上一次的更新时间是两年前,由于项目使用,就等不到作者更新了 1. 解决方法(纯属个人) 首先,警告里面给出了解决方式: 使用collection.countDocument方法或collection.estimatedDocumentCount,那么使用哪一个更好呢.先看看官方给出这两个方法的介绍吧. count方法(已废弃): http://mongoosejs.com/docs/api.html#model_Model.count count.png

countDocuments方法: http://mongoosejs.com/docs/api.html#model_Model.countDocuments countDocument.jpg

estimatedDocumentCount方法: http://mongoosejs.com/docs/api.html#model_Model.estimatedDocumentCount 1532681984(1).jpg

官方建议是使用estimatedDocumentCount方法,但是estimatedDocumentCount不能使用查询条件,所以根据项目中实际场景选择countDocuments还是estimatedDocumentCount.那么在什么地方替换掉count方法呢? 找到项目目录下或者全局目录文件: node_modules/mongoose-paginate/index.js 微信截图_20180727170313.png

将上图中红色箭头指向的位置count替换为estimatedDocumentCount或countDocuments,当然了,这种修改方式纯属个人意见,肯定还有其他解决方式.

2 回复

estimatedDocumentCount方法不可以使用查询条件了吧 image.png because estimatedDocumentCount() uses collection metadata rather than scanning the entire collection

@jiangli373 是的是的,实在是惭愧,没有深入探究,多谢大神指正,今天在项目上也发现了这个问题,我随后改用了countDocuments方法,

回到顶部