关于c driver取得执行结果的问题
我现在有一个需求,求表中一字段的累加之和,表结构及mapreduce执行结果如下:
shard1:PRIMARY> db.mytest.find()
{ "_id" : 1, "name" : "apple", "price" : 10 }
{ "_id" : 2, "name" : "boy", "price" : 5 }
{ "_id" : 3, "name" : "apple", "price" : 20 }
{ "_id" : 4, "name" : "apple", "price" : 30 }
{ "_id" : 5, "name" : "cup", "price" : 10 }
{ "_id" : 6, "name" : "cup", "price" : 20 }
{ "_id" : 7, "name" : "dark", "price" : 10 }
{ "_id" : 8, "name" : "dark", "price" : 10 }
{ "_id" : 9, "name" : "egg", "price" : 10 }
{ "_id" : 0, "name" : "film", "price" : 10 }
shard1:PRIMARY> db.runCommand({ mapreduce:'mytest', map:function() { emit(1,{sum:this.price}); }, reduce:function(key, values) { var a = values[0]; for (var i=1;i < values.length; i++) { var b = values[i]; a.sum += b.sum; } return a; }, out:{inline:1}})
{
"results" : [
{
"_id" : 1,
"value" : {
"sum" : 135
}
}
],
"timeMillis" : 1,
"counts" : {
"input" : 10,
"emit" : 10,
"reduce" : 1,
"output" : 1
},
"ok" : 1
}
我现在需要在c代码中取到sum的值,该如何做?
如下是无法取到sum值的:
bson cmd;
bson out = {NULL, 0};
bson_init(&cmd);
bson_append_string(&cmd, "mapreduce", "mytest");
bson_append_string(&cmd, "map", "function(){ emit(1,{sum:this.a_price}); }");
bson_append_string(&cmd, "reduce", "function(key, values){ var a = values[0]; for (var i=1;i < values.length; i++) { var b = values[i]; a.sum += b.sum; } return a; }");
bson_append_string(&cmd, "out", "{outline:1}");
bson_finish(&cmd);
const int result = mongo_run_command(conn, "mydb", &cmd, &out);
bson_print((const bson *)&out);
bson_print的结果是:
result : 2 {outline:1}
timeMillis : 16 56
counts : 3
input : 16 6
emit : 16 6
reduce : 16 1
output : 16 1
ok : 1 1.000000
shard1:PRIMARY> db.mytest.find()
{ "_id" : 1, "name" : "apple", "price" : 10 }
{ "_id" : 2, "name" : "boy", "price" : 5 }
{ "_id" : 3, "name" : "apple", "price" : 20 }
{ "_id" : 4, "name" : "apple", "price" : 30 }
{ "_id" : 5, "name" : "cup", "price" : 10 }
{ "_id" : 6, "name" : "cup", "price" : 20 }
{ "_id" : 7, "name" : "dark", "price" : 10 }
{ "_id" : 8, "name" : "dark", "price" : 10 }
{ "_id" : 9, "name" : "egg", "price" : 10 }
{ "_id" : 0, "name" : "film", "price" : 10 }
shard1:PRIMARY> db.runCommand({ mapreduce:'mytest', map:function() { emit(1,{sum:this.price}); }, reduce:function(key, values) { var a = values[0]; for (var i=1;i < values.length; i++) { var b = values[i]; a.sum += b.sum; } return a; }, out:{inline:1}})
{
"results" : [
{
"_id" : 1,
"value" : {
"sum" : 135
}
}
],
"timeMillis" : 1,
"counts" : {
"input" : 10,
"emit" : 10,
"reduce" : 1,
"output" : 1
},
"ok" : 1
}
我现在需要在c代码中取到sum的值,该如何做?
如下是无法取到sum值的:
bson cmd;
bson out = {NULL, 0};
bson_init(&cmd);
bson_append_string(&cmd, "mapreduce", "mytest");
bson_append_string(&cmd, "map", "function(){ emit(1,{sum:this.a_price}); }");
bson_append_string(&cmd, "reduce", "function(key, values){ var a = values[0]; for (var i=1;i < values.length; i++) { var b = values[i]; a.sum += b.sum; } return a; }");
bson_append_string(&cmd, "out", "{outline:1}");
bson_finish(&cmd);
const int result = mongo_run_command(conn, "mydb", &cmd, &out);
bson_print((const bson *)&out);
bson_print的结果是:
result : 2 {outline:1}
timeMillis : 16 56
counts : 3
input : 16 6
emit : 16 6
reduce : 16 1
output : 16 1
ok : 1 1.000000
...全文
相关推荐
MongoDB C Driver 1.13.0
window 下mongodb 64 C 驱动debug 版库文件, 版本是最新的MongoDB C Driver 1.13.0
dx_driver_test.zip
在写完安卓驱动后,为了方便测试其驱动的功能,可以像linux一样写一个C的测试文件,然后编译成可执行文件push到机器中执行。
(Oracle Database 12c Release 1 JDBC Driver)demo.zip
Oracle Database 12c Release 1 JDBC Driver demo.zip
mongo-c-driver-win32-debug-staticlib
mongodb数据库c开发库,windows 32位 debug的静态库,
Cassandra cpp Driver 2.1
C/C++ Driver 2.1 for Apache Cassandra Document August 13, 2015


