MongoDB $group command in c++
MongoDB $group command in c++
我有问题得到mongodb的$group命令在c++ (Qt)工作
文档的示例代码按预期工作并返回结果:
db.article.aggregate(
{ "$group" : {
"_id" : "$author",
"docsPerAuthor" : { "$sum" : 1 }
}}
);`
转换为c++返回一个空结果集,但没有错误:
QString queryCommand = "{ group : {"
"_id : "$author", "
"docsPerAuthor : {$sum : 1} "
"}}";
BSONObj bson_query_result = m_mongoConnection.findOne("data.collection",
fromjson(queryCommand.toStdString().c_str()));
std::cout << "Output: " << bson_query_result.toString() << std::endl;
聚合是通过"runCommand"方法调用的,该方法接受数据库名称和包含命令和集合的BSONObj,以及实际的管道(数组)。最后一个参数是响应对象。全部文档。
假设您有一个DBClientConnection m_mongoConnection并使用"test"数据库:
BSONObj res;
BSONArray pipeline = BSON_ARRAY(
BSON( "$group" <<
BSON( "_id" << "$author" ) <<
BSON( "docsPerAuthor" <<
BSON( "$sum" << 1 )
)
)
);
m_mongoConnection.runCommand(
"test", BSON(
"aggregate" << "article" << "pipeline" << pipeline
),
res
);
cout << res.toString() << endl
如何构造BSON参数取决于您的个人品味。
相关文章:
- netcat command in c++
- Difference in displaying cv2 Mat
- C++ MFC Libraries in Travis CI
- 如何在OpenSSL中从configuration.h.in获取configuration.h
- 创建具有 new in 函数和"this is nullptr"异常的对象
- IN, OUT, INOUT Parameters
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 有人安装"IITB Simplecpp in mac"吗?
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 如何修复"error: ‘_1’ was not declared in this scope"?
- Softmax Implementation in C++
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- IF-nesting in c++
- Gurobi GRBModel and GRBmodel in C++
- Tensorflow Hub in C++
- Centos7 g++ "to_string is not in a member of std"
- InitializeCriticalSectionEx Not Located In KERNEL32.Dll
- 将 lambda 表达式传递给 std::function in C++
- @CPPFLAGS@在 Makefile.in 中意味着什么?
- std::async from std::async in windows xp