Mongo Java 驱动程序不遵守限制方法 [英] Mongo Java driver not obeying limit method

查看:47
本文介绍了Mongo Java 驱动程序不遵守限制方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询

DBCursor mongoCursor = mongoCollection.find(query).sort(sort).limit(5000);
long mongoCursorCount = mongoCursor.count();
myLogger.info("mongoCursorCount " + mongoCursorCount);

显示 120 万个文档的 mongoCursorCount 值

which is showing a mongoCursorCount value of 1.2M docs

limit 方法适用于 shell 查询,但它是否通过 mongo-java-driver-2.1.1.jar 驱动程序工作?

The limit method works in shell queries but does it work via the mongo-java-driver-2.1.1.jar driver?

推荐答案

默认情况下,MongoDB 忽略 limitskip 当它们与 count.要更改此行为,您需要将 applySkipLimit 设置为 true.请参阅此处了解更多详情.

By default MongoDB ignores limit and skip when they are applied together with count. To change this behavior you need to set applySkipLimit to true. See here for more details.

在 Mongo shell 中,这将默认忽略 limitskip:

In Mongo shell this will ignore the limit and skip by default:

db.coll.find().limit(3).count();

这将尊重这些修饰符:

db.coll.find().limit(3).count(true);

您可能使用的是旧版本的 Mongo shell,而这不是默认的或类似的.

It could be possible that you are using an older version of Mongo shell where this is not default or something like that.

要修复您的 Java 代码,请使用 size() 而不是 count() - 这就像调用 count(true) 而不是 >count() 在 shell 中.

To fix your Java code use size() instead of count() - that's like calling count(true) instead of count() in shell.

这篇关于Mongo Java 驱动程序不遵守限制方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆