MongoDB c++驱动查询错误

MongoDB C++ driver query error

本文关键字:查询 错误 c++ MongoDB      更新时间:2023-10-16

MongoDB c++驱动程序正在做我的头。我有一个工作驱动程序,它可以很好地用于所有查询,除了下面的查询,它会停止程序编译。

代码应该根据MongoDB文档工作:http://docs.mongodb.org/ecosystem/drivers/cpp-to-sql-to-mongo-shell/

目的是只选择集合中所有文档的"name"字段。

using namespace std;
using namespace mongo;
DBClientConnection c;
c.connect("localhost");
auto_ptr<DBClientCursor> cursor = c.query("db.coll", Query(), 0, 0, BSON("name" << 1));
//auto_ptr<DBClientCursor> cursor = c.query("db.coll", Query().sort("_id", -1), 0, 0);
//auto_ptr<DBClientCursor> cursor = c.query("db.coll", Query());
//auto_ptr<DBClientCursor> cursor = c.query("db.coll", QUERY("type" << "blog"));

但是,我得到以下错误:

error: no matching function for call to ‘mongo::DBClientConnection::query(const char [19], mongo::Query, int, int, mongo::BSONObj)’
mongodriver/include/mongo/client/dbclientinterface.h:1274: note: candidates are:       virtual std::auto_ptr<mongo::DBClientCursor> mongo::DBClientConnection::query(const std::string&, mongo::Query, int, int, const mongo::BSONObj*, int, int)

我已经尝试了我能想到的所有方法来使调用参数与建议的候选参数匹配,但只成功地生成了不同的错误。注意,注释掉的查询都可以正常工作。令人沮丧。

提前感谢你的真知灼见

@mjhall,

我相信你指出上述查询不起作用是正确的。当示例传递一个BSONObj时,c.query()期望一个BSONObj*。你能试着跟着做,看看是否对你有用吗?

DBClientConnection c;
c.connect("localhost");
BSONObj b = BSON("name" << 1);
auto_ptr<DBClientCursor> cursor = c.query("db.coll", Query(), 0, 0, &b);