【发布时间】:2018-08-03 06:58:06
【问题描述】:
我有一个如下的 mongo DB 查询
db.getCollection('ABC_COLLECTION_01').aggregate
([{ "$group" : {
"_id" : 0,
"total" : { "$sum" : "$columA" },
"total_sub" : { "$sum" : {$cond:{ if: { $gte: [ "$columnB", new ISODate("2018-01-01T04:58:09.000+0100") ] }, then: "$columA", else: 0 }}}
}}])
此查询运行良好。如果我在 ABC_COLLECTION_01 上运行它,它将返回类似的结果(仅作为示例)
total = 250 & total_sub = 120
现在我必须使用 mongo::BSONArrayBuilder 在 C++ 代码中编写此查询,如下所示
//Calculate 2 sum
aBuilder.append(BSON("$group"<<BSON("_id"<<0
<<"total"<<BSON("$sum"<<'$' +columA)
<<"total_sub"<<BSON("$sum"<<'$cond'<<'if'<<'$' +columB<<"$gte"<<rmsmongo::utils::Adaptor::ToMongoDate(StartTime,true)<<'then'<<'$' +columA<<'else'<<0))));
mongo::BSONArray New_AggregationQuery = aBuilder.arr();
std::auto_ptr<dsclient::Cursor> Cursor = _MyCollection.aggregate(New_AggregationQuery, dsclient::Option_aggregate().retry(dsclient::Retry(dsclient::ExponentialRetry, 2)).maxTimeMS(200000));
如果您看到我为 $total_sub 编写的 $cond 在 C++ 代码中是错误的 - 它不起作用。 你能帮我纠正一下吗? 提前致谢
【问题讨论】:
标签: c++ mongodb mongodb-query bson