蒙戈C++;驱动程序查询()方法没有获得它应该得到的所有结果 我使用Mongo C++驱动程序(LeaCy1.1.0.2版)使用查询方法,遇到了一些奇怪的行为。p>
特别是,我有一个给定的数据库(蒙戈C++;驱动程序查询()方法没有获得它应该得到的所有结果 我使用Mongo C++驱动程序(LeaCy1.1.0.2版)使用查询方法,遇到了一些奇怪的行为。p>,c++,mongodb,C++,Mongodb,特别是,我有一个给定的数据库(orion)(其中包含一些填充的数据)和一个程序,该程序使用四种不同的方法为给定的查询表达式计算该数据库的给定集合(entities)中的元素: 计数法 普通查询(即没有额外的跳过参数限制) 使用大于集合中最大元素数的限制进行查询(我使用1000,集合中有886个元素) 使用相同限制进行查询并跳到0 程序代码: #include <cstdlib> #include <iostream> #include "mongo/client/db
orion
)(其中包含一些填充的数据)和一个程序,该程序使用四种不同的方法为给定的查询表达式计算该数据库的给定集合(entities
)中的元素:
- 计数法
- 普通查询(即没有额外的跳过参数限制)
- 使用大于集合中最大元素数的限制进行查询(我使用1000,集合中有886个元素)
- 使用相同限制进行查询并跳到0
#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver
// Compilation hint: g++ example.cpp -pthread -lmongoclient -lboost_thread -lboost_system -lboost_regex -o example
using namespace mongo;
int main() {
client::initialize();
DBClientConnection c;
std::auto_ptr<DBClientCursor> cursor;
c.connect("localhost");
BSONObj q = BSON("_id.servicePath" << BSON ( "$in" << BSON_ARRAY( "/qa_fermin") ) );
int n1 = c.count("orion.entities", q);
cursor = c.query("orion.entities", q);
int n2 = cursor->itcount();
cursor = c.query("orion.entities", q, 1000);
int n3 = cursor->itcount();
cursor = c.query("orion.entities", q, 1000, 0);
int n4 = cursor->itcount();
std::cout << "using count: " << n1 << std::endl;
std::cout << "plain query: " << n2 << std::endl;
std::cout << "query + limit: " << n3 << std::endl;
std::cout << "query + limit + skip: " << n4 << std::endl;
}
最后两种情况(query+limit和query+limit+skip)检索的文档似乎比预期的少
据我所知,无论查询表达式和数据库如何,只要限制值大于集合中的文档总数,这4种情况下的结果都应该是相同的
我有一种“感觉”,它可能与游标在某种程度上的行为有关,但我不知道如何。。。任何有助于理解这种行为的想法都将不胜感激
编辑:我“隔离”了步骤2和步骤3,并查看MongoDB服务器日志(查询子系统的详细级别设置为5)。我正在添加跟踪,以防它们有用(省略前缀时间戳,使它们更短):
对于普通查询(返回307个结果的查询):
(看起来第一批47,第二批188,最后一批72,47+188+72=307)
如果查询+限制(错误返回188的):
(只有一个批次,其大小与“righ query”案例中scond批次的大小非常接近)。您可以尝试升级到legacy-1.0.6或更高版本吗?该版本修复了与游标管理相关的错误()如果您还不想跳转到legacy-1.1系列,我建议升级到legacy-1.1.0或legacy-1.0.7。
using count: 307
plain query: 307
query + limit: 188
query + limit + skip: 188
D QUERY [conn108926] Enough for first batch, wantMore=1 ntoreturn=0 numResults=47
D QUERY [conn108926] caching executor with cursorid 104197639043543 after returning 47 results
...
D QUERY [conn108926] Running getMore, cursorid: 104197639043543
D QUERY [conn108926] getMore saving client cursor ended with state ADVANCED
D QUERY [conn108926] getMore returned 188 results
...
D QUERY [conn108926] Running getMore, cursorid: 104197639043543
D QUERY [conn108926] getMore NOT saving client cursor, ended with state IS_EOF
D QUERY [conn108926] getMore returned 72 results
D QUERY [conn108927] Enough for first batch, wantMore=0 ntoreturn=1000 numResults=188
D QUERY [conn108927] Not caching executor but returning 188 results.