0

示例数据如下所示。

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

使用 mongo cxx 驱动程序,我们如何重现上述相同的结果?

4

1 回答 1

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::builder::basic::make_document() 和 bsoncxx::builder::basic::make_array()),允许深入编写以比上面的示例更紧凑的方式嵌套 BSON 文档。请参阅https://jira.mongodb.org/browse/CXX-1174

于 2017-04-19T10:37:04.677 回答