在mongocxx API中,Collection.aggregate()需要一个流水线对象才能运行聚合管道查询。这意味着要使用Pipeline类构建查询。例如:
有没有一种方法可以通过传递一个字符串在mongocxx中运行聚合管道查询?我不想使用mongocxx对象构造查询,而是运行字符串查询。
例如:
mongocxx::pipeline p{};
p.match(make_document(kvp("items.fruit", "banana")));
p.sort(make_document(kvp("date", 1)));
auto cursor = db["sales"].aggregate(p, mongocxx::options::aggregate{});
有没有一种方法可以通过传递一个字符串在mongocxx中运行聚合管道查询?我不想使用mongocxx对象构造查询,而是运行字符串查询。
例如:
db["sales"].aggregate("[{"$match": { ... }}"]
其中"[{"$match": { ... }}"是一种类型为std::string的管道聚合查询。
bsoncxx::from_json
将管道组件(如匹配、排序等)解析为 JSON 片段,而不是整个字符串。 - rustyx