2017-04-18 3 views
0

Beispieldaten sind wie folgt.cxx MongoDB-Gruppe mit multipler ID

{ "_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 verwenden, können wir die Aggregation Abfrage wie

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 } 
     } 
     } 
    ] 
) 

Das Ergebnis sind

{ "_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 } 

Mit Mongo cxx Fahrer tun, wie können wir gleichen Ergebnisse über reproduzieren?

Antwort

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

Beachten Sie auch, dass die mongocxx 3.2.0 Release neue Basis builder Helfer einführen (vorläufig genannt bsoncxx :: Builder :: Grund :: make_document() und bsoncxx :: Builder :: Grund :: make_array()), die es ermöglichen, tief verschachtelte BSON-Dokumente kompakter als im obigen Beispiel zu schreiben. Siehe https://jira.mongodb.org/browse/CXX-1174.