2017-04-18 27 views
0

示例數據如下所示。cxx具有多個ID的MongoDB組

{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") } 
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") } 
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") } 
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") } 
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") } 

使用JavaScript,我們可以做的聚集查詢像

db.sales.aggregate(
    [ 
     { 
     $group : { 
      _id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } }, 
      totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } }, 
      averageQuantity: { $avg: "$quantity" }, 
      count: { $sum: 1 } 
     } 
     } 
    ] 
) 

結果是

{ "_id" : { "month" : 3, "day" : 15, "year" : 2014 }, "totalPrice" : 50, "averageQuantity" : 10, "count" : 1 } 
{ "_id" : { "month" : 4, "day" : 4, "year" : 2014 }, "totalPrice" : 200, "averageQuantity" : 15, "count" : 2 } 
{ "_id" : { "month" : 3, "day" : 1, "year" : 2014 }, "totalPrice" : 40, "averageQuantity" : 1.5, "count" : 2 } 

使用蒙戈CXX司機,我們怎麼能重現上述同樣的結果?

回答

1
#include <iostream> 

#include <bsoncxx/builder/basic/document.hpp> 
#include <bsoncxx/builder/basic/kvp.hpp> 
#include <bsoncxx/json.hpp> 
#include <mongocxx/client.hpp> 
#include <mongocxx/instance.hpp> 
#include <mongocxx/pipeline.hpp> 

using bsoncxx::builder::basic::kvp; 
using bsoncxx::builder::basic::sub_array; 
using bsoncxx::builder::basic::sub_document; 

int main(int, char**) { 
    // The mongocxx::instance constructor and destructor initialize and shut down the driver, 
    // respectively. Therefore, a mongocxx::instance must be created before using the driver and 
    // must remain alive for as long as the driver is in use. 
    mongocxx::instance inst{}; 
    mongocxx::client conn{mongocxx::uri{}}; 
    auto coll = conn["test"]["sales"]; 

    bsoncxx::builder::basic::document group_doc; 

    group_doc.append(
     kvp("_id", 
      [](sub_document id) { 
       id.append(
        kvp("month", [](sub_document month) { month.append(kvp("$month", "$date")); }), 
        kvp("day", [](sub_document day) { day.append(kvp("$dayOfMonth", "$date")); }), 
        kvp("year", [](sub_document year) { year.append(kvp("$year", "$date")); })); 
      }), 
     kvp("totalPrice", 
      [](sub_document total_price) { 
       total_price.append(kvp("$sum", [](sub_document sum) { 
        sum.append(kvp("$multiply", [](sub_array multiply) { 
         multiply.append("$price", "$quantity"); 
        })); 
       })); 
      }), 
     kvp("averageQuantity", 
      [](sub_document average_quantity) { 
       average_quantity.append(kvp("$avg", "$quantity")); 
      }), 
     kvp("count", [](sub_document count) { count.append(kvp("$sum", 1)); })); 

    auto cursor = coll.aggregate(mongocxx::pipeline{}.group(group_doc.extract())); 
    for (auto&& doc : cursor) { 
     std::cout << bsoncxx::to_json(doc) << std::endl; 
    } 
} 

還要注意mongocxx 3.2.0版本將引入新的基本建設者傭工(暫稱bsoncxx ::製造商::基本:: make_document()和bsoncxx ::製造商::基本:: make_array() ),它允許以比上例更緊湊的方式寫出深嵌套的BSON文檔。請參閱https://jira.mongodb.org/browse/CXX-1174