【问题标题】:How to Join a Collection to a Document using Mongocxx如何使用 Mongocxx 将集合加入文档
【发布时间】:2019-01-21 21:32:29
【问题描述】:

我有一个名为“main”的文档(或集合,以最适合获得解决方案的为准):

{
    "field1":"value1",
    "objects":[ {"key":1}, {"key":2} ]
}

我有另一个名为“foreign”的集合,其中包含以下三个文档:

//document 1
{
    "foreignKey":1,
    "name": "Mike"
}
//document 2     {
    "foreignKey":2,
    "name": "Michael"
}
//document 3
{
    "foreignKey":3,
    "name": "Mick"
}

我希望合并的结果是:

//results
{
    "field1":"value1",
    "objects":[ 
        {
            "foreignKey":1,
            "name": "Mike"
        }, 
        {
            "foreignKey":2,
            "name": "Michael"
        }
    ]
}

我只在MongoDB 中找到了几乎可以完成此任务的示例,但它的示例只有一个值数组;但我有一个对象数组。

我不知道怎么翻译成Mongo-cxx

为方便起见,我从MongoDB site 复制了以下示例

------------------------------------------------
//Consider a collection orders with the following //document:
({ "_id" : 1, "item" : "MON1003", "price" : 350, "quantity" : 2, "specs" :
[ "27 inch", "Retina display", "1920x1080" ], "type" : "Monitor" }

//Another collection inventory contains the following //documents:
{ "_id" : 1, "sku" : "MON1003", "type" : "Monitor", "instock" : 120,
"size" : "27 inch", "resolution" : "1920x1080" }
{ "_id" : 2, "sku" : "MON1012", "type" : "Monitor", "instock" : 85,
"size" : "23 inch", "resolution" : "1280x800" }
{ "_id" : 3, "sku" : "MON1031", "type" : "Monitor", "instock" : 60,
"size" : "23 inch", "display_type" : "LED" }
(
//The following aggregation operation performs a join //on documents in the orders collection which match a //particular element of the specs array to the size //field in the inventory collection.
db.orders.aggregate([
   //stage
   {
      $unwind: "$specs"
   },
   //stage
   {
      $lookup:
         {
            from: "inventory",
            localField: "specs",
            foreignField: "size",
            as: "inventory_docs"
        }
   },
   //stage
   {
      $match: { "inventory_docs": { $ne: [] } }
   }
])

//The operation returns the following document:
{
   "_id" : 1,
   "item" : "MON1003",
   "price" : 350,
   "quantity" : 2,
   "specs" : "27 inch",
   "type" : "Monitor",
   "inventory_docs" : [
      {
         "_id" : 1,
         "sku" : "MON1003",
         "type" : "Monitor",
         "instock" : 120,
         "size" : "27 inch",
         "resolution" : "1920x1080"
      }
   ]
}

【问题讨论】:

    标签: join aggregate lookup aggregation mongo-cxx-driver


    【解决方案1】:

    使用您从 MongoDB 的 $lookup with an array 链接的示例,并基于 aggregation_examples.cpptest/collection.cpp,以下代码使用 $unwind、$lookup 和 $match 实现聚合操作,该操作返回与示例中相同的文档:

    #include <iostream>
    
    #include <bsoncxx/json.hpp>
    
    #include <mongocxx/client.hpp>
    #include <mongocxx/instance.hpp>
    #include <mongocxx/uri.hpp>
    
    using bsoncxx::builder::basic::kvp;
    using bsoncxx::builder::basic::make_document;
    using bsoncxx::builder::basic::array;
    
    int main(int, char**)
    {
        std::cout << "Start program" << std::endl;
    
        mongocxx::instance instance{};
        mongocxx::client client{ mongocxx::uri{} };
    
        mongocxx::database db = client["stack"];
        mongocxx::collection colInventory = db["inventory"];
    
        // Pipeline stages: $unwind, $lookup and $match
        mongocxx::pipeline pipe{};
        pipe.unwind("$specs");
        pipe.lookup(
            make_document(
                kvp("from",         colInventory.name()),
                kvp("localField",   "specs"),
                kvp("foreignField", "size"),
                kvp("as",           "inventory_docs")
            )
        );
        pipe.match(
            make_document(
                kvp("inventory_docs", make_document(kvp("$ne", array{})))
            )
        );
    
        auto cursor = db["orders"].aggregate(pipe, mongocxx::options::aggregate{});
    
        for (auto doc : cursor) {
            std::cout << bsoncxx::to_json(doc) << std::endl;
        }
    
        std::cout << "End program" << std::endl;
    }
    

    输出:

    Start program
    { "_id" : 1.0, "item" : "MON1003", "price" : 350.0, "quantity" : 2.0, "specs" : "27 inch", "type" : "Monitor", "inventory_docs" : [ { "_id" : 1.0, "sku" : "MON1003", "type" : "Monitor", "instock" : 120.0, "size" : "27 inch", "resolution" : "1920x1080" } ] }
    End program
    

    漂亮的印刷品:

    {
        "_id": 1.0,
        "item": "MON1003",
        "price": 350.0,
        "quantity": 2.0,
        "specs": "27 inch",
        "type": "Monitor",
        "inventory_docs": [
            {
                "_id": 1.0,
                "sku": "MON1003",
                "type": "Monitor",
                "instock": 120.0,
                "size": "27 inch",
                "resolution": "1920x1080"
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多