简体   繁体   English

当我们使用带有限制选项的count时,为什么mongo db(版本3.0.6)返回错误的记录数?

[英]Why mongo db (version 3.0.6) returns wrong number of records when we use count with limit option?

As per mongo db doc says we can use count with limit. 正如mongo db doc所说,我们可以将count与limit一起使用。

Limit option is used to specify the maximum number of documents the cursor will return. 限制选项用于指定光标将返回的最大文档数。 But if we use limit with count it returns total count and not correct count. 但是,如果我们将限制与计数一起使用,它将返回总计数,而不是正确的计数。

Why? 为什么?

Suppose we have 50 records in collection then only count option will return 50, and if we apply limit(10) option then it should return 10 and not 50. But count with limit returns 50. 假设我们在集合中有50条记录,那么只有count选项将返回50,如果我们应用limit(10)选项,则它将返回10而不是50。但是带有limit的计数将返回50。

db.collection.find(<query>).count();

You will get count of all records found after executing the query. 执行查询后,您将获得所有找到的记录的计数。 ie count=50; 即计数= 50;

db.collection.find(<query>).limit(10).count(true);

You will get the count of limited documents. 您将获得有限文件的数量。 ie count=10. 即计数= 10。

You should set applySkipLimit to true. 您应该将applySkipLimit设置为true。

http://docs.mongodb.org/manual/reference/method/cursor.count/ http://docs.mongodb.org/manual/reference/method/cursor.count/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 适用于Robot Framework的Mongo Db扩展库返回错误记录 - Mongo Db extended library for Robot Framework returns wrong records 当与find选项一起使用时,Mongo DB将根据什么显示记录? - On What basis does the Mongo DB displays the records when used with find option? 我们可以在 mongo DB 上使用弹性搜索吗? - can we use elastic search on mongo DB? 在mongo db中使用Java驱动程序时,如何限制结果数? - How do I limit the number of results when using the Java driver for mongo db? 为什么 db.user.count() 的结果在 mongo 中是 0? - Why the result of db.user.count() is 0 in mongo? 当有数百万条记录时,Mongo计数真的很慢 - Mongo count really slow when there are millions of records mongo db-获取具有其他集合计数的表记录 - mongo db - get table records with count of another collection 当我们将Mongoose与Express结合使用时,默认情况下数据将存储在Mongo DB中的位置 - Where data will be stored in Mongo DB by default, when we use mongoose with Express 即使使用限制,Rails Mongoid模型查询结果也会返回错误的大小/长度/计数信息 - Rails Mongoid model query result returns wrong size/length/count info even when using limit Mongo DB 跳过和限制 - Mongo DB skip and limit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM