2017-04-18 5 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") } 

자바 스크립트를 사용하여, 우리는 우리가 위의 같은 결과를 재현 할 수있는 방법,

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 드라이버를 사용하고 그 결과 같은 집계 쿼리를 할 수 있습니까? mongocxx 3.2.0 릴리스는 새로운 기본 빌더 도우미를 소개하는 것 또한

답변

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; 
    } 
} 

주 (가칭라는 bsoncxx :: 빌더 :: 기본 :: make_document()와 bsoncxx :: 빌더 :: 기본 :: make_array()) 위의 예보다 더 간결하게 중첩 된 BSON 문서를 작성할 수 있습니다. https://jira.mongodb.org/browse/CXX-1174을 참조하십시오.