查询所有字段

Query for all fields

我有一个 mongo 集合,看起来像这样

db.SymbolMCIpPort.find()
{ "_id" : ObjectId("59f7bdaee23020650635ed3c"), "Provider" : "BROKER1", "Symbol" : "EURUSD", "MCIpPort" : { "IP" : "224.0.0.1", "PORT" : "12345" } }
{ "_id" : ObjectId("59f7bdaee23020650635ed3d"), "Provider" : "BROKER1", "Symbol" : "EURJPY", "MCIpPort" : { "IP" : "224.0.0.1", "PORT" : "12346" } }

我正在尝试查询它以找到匹配 EURUSD 的所有字段,如下所示:

// Create the query filter
auto filter = document{} << "Symbol" << "EURUSD"
                             << finalize;
// Create the find options with the projection
mongocxx::options::find opts{};
opts.projection(document{} << "_id" << "Provider" << "Symbol" << 1 << finalize);

// Execute find with options
auto cursor = coll.find(filter.view(), opts);
for (auto &&doc : cursor) {
    std::cout << bsoncxx::to_json(doc) << std::endl;
}

但它只让我得到两个字段,idSymbol [我什至很困惑为什么我不也得到 Provider 字段,更不用说其余的了匹配 EURUSD 的字段。]

{ "_id" : { "$oid" : "59f7bdaee23020650635ed3c" }, "Symbol" : "EURUSD" }

如何修改代码获取所有字段?

我用 find_one 稍微不同地解决了它:

auto filter = document{} << "Symbol" << rawSymbol
                                     << finalize;

bsoncxx::stdx::optional<bsoncxx::document::value> maybe_result = coll.find_one(filter.view());
if(maybe_result) {
    std::cout << bsoncxx::to_json(*maybe_result) << "\n";
}