- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 MongoDB,更具体地说是 Mongoose。我最近已经摆脱了更新 mongo 和 mongoose 的一些弃用警告。
我的一些查询可能如下所示:
const count = await Badge.find(query).count().exec()
从那以后,我将其更改为
const count = Badge.find(query).countDocuments().exec()
和中提琴贬低警告消失了。但是,阅读 MongoDB 上的文档似乎应该这样写:
const count = Badge.countDocuments(query)
以上所有返回完全相同的东西。显然后两个是我想要的,因为
count()
的弃用。
最佳答案
db.collection.find
方法返回一个游标。游标上的 cursor.count() 方法计算游标引用的文档数。这与 db.collection.count()
相同。
从 MongoDB v4.0 开始,这两种方法( cursor.count()
和 db.collection.count()
)都已弃用。从 documentation :
MongoDB drivers compatible with the 4.0 features deprecate theirrespective cursor and collection count() APIs in favor of new APIs forcountDocuments() and estimatedDocumentCount(). For the specific APInames for a given driver, see the driver documentation.
Avoid using the db.collection.count() method without a query predicatesince without the query predicate, the method returns results based onthe collection’s metadata, which may result in an approximate count.
All of the above return the exact same thing.
countDocuments
返回文档的实际计数。其他方法根据集合的元数据返回计数。
db.collection.count
,请将其与查询谓词一起使用,这将返回文档的确切计数(但请注意,此方法已弃用)。
关于MongoDB count() 与 countDocuments(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65282889/
我正在使用 MongoDB,更具体地说是 Mongoose。我最近已经摆脱了更新 mongo 和 mongoose 的一些弃用警告。 我的一些查询可能如下所示: const count = await
我开始学习 mongoose/MongoDB 聚合函数,但遇到了一些基本困难。例如,我正在尝试执行以下操作: var myModels= require('./models/myModel'); va
我开始学习 mongoose/MongoDB 聚合函数,但遇到了一些基本困难。例如,我正在尝试执行以下操作: var myModels= require('./models/myModel'); va
我有一个具有以下结构的文档: { '_id': '', 'a': '', 'b': '', 'c': [ { '_id': ''
我对 Javascript 还很陌生,我不明白为什么我无法保存下面这段代码的结果。我正在使用 MongoDB,我想存储集群中的文档数量,以便我可以跟踪集群中有多少文档,但是,当我尝试在第三行记录输出时
因此,我正在为 go go.mongodb.org/mongo-driver 使用官方的 mongo 驱动程序,并且在容器中运行 mongodb v4。所有操作都运行良好,但当我尝试向 CountDo
我是一名优秀的程序员,十分优秀!