代码之家  ›  专栏  ›  技术社区  ›  Gowri

在数组中连接查找对象ID

  •  4
  • Gowri  · 技术社区  · 9 年前

    我有一个商业集合,其中包含类别对象id的名称和数组以及品牌对象id的数组。

    • 业务和类别是oneToMany

    示例集合结构

    {
    "_id":ObjectId("595f2311f43c42124360a71f"),
    "name":"Women",
    }
    {
    "_id":ObjectId("595f2311f43c42124360a71e"),
    "name":"MEN",
    }
    

    品牌

    {
    "_id":ObjectId("695f2311f43c42124360a71f"),
    "name":"Brand A",
    }
    {
    "_id":ObjectId("695f2311f43c42124360a71e"),
    "name":"Brand B",
    }
    

    {
     "_id":ObjectId("59a7d9e2d290a654c53bb1b6"),
     "name":"My Store",
    "brands":[
     ObjectId("596e56489658851024160544"),
     ObjectId("597831971cc07f51bdaabfe6")
    ],
    "categories":[
     ObjectId("595f2311f43c42124360a720"),
     ObjectId("59780cf7bb23af4eced57dba"),
     ObjectId("597f63f642c77654e1c8c574")
    ]
    
    }
    

    从上面的结构尝试使用以下格式公开数据 db.createView . 所以我试图查找categories集合和brand集合,以在数组中显示名称,而不是对象ID。

    预期输出

    {
     "_id":ObjectId("59a7d9e2d290a654c53bb1b6"),
     "j_name":"My Store",
    "brands":[
     "Brand A",
     "Brand B",
    
    ],
    "categories":[
     "MEN",
     "WOMEN"
    ]    
    }
    

    db.businesses.aggregate([
    
           { $unwind: {path:"$categories",preserveNullAndEmptyArrays: true}},
           { $lookup: {
               "from": "categories",
               "localField": "categories",
               "foreignField": "_id",
               "as": "categoryObjects"
    
                }
            },  
            { $unwind: {path:"$categoryObjects",preserveNullAndEmptyArrays: true}},
            // Group back to arrays
            { "$group": {
                "_id": "$_id",          
                "categoryObjects": { "$push": "$categoryObjects.name" }
            }},     
    
            { $project : { 
                "_id" : "$_id","j_name" :"$name","j_keywords" : "$keywords","rating":"$rating.overAll","logo":"$logo",
                "j_l_city" : "$address.city","j_l_area" : "$address.area","location":{$concat : ["$address.lat", ",", "$address.lng"]},
                "attr_payments":"$attributes.payments","attr_delivery":"$attributes.delivery","attr_parking":"$attributes.parking",         "attr_locatedAt":"$attributes.locatedAt","attr_tailoring":"$attributes.tailoring","attr_wifi":"$attributes.wifi","attr_kidspark":"$attributes.kidspark",
                "j_categories":"$categoryObjects",
                 }}
             ])
    
    1 回复  |  直到 9 年前
        1
  •  4
  •   chridam Gino Claudi    8 年前

    更好的管道是:

    db.business.aggregate([
        { "$lookup": {
            "from": "categories",
            "localField": "categories",
            "foreignField": "_id",
            "as": "categories"
        } },
        { "$lookup": {
            "from": "brands",
            "localField": "brands",
            "foreignField": "_id",
            "as": "brands"
        } },
        { "$addFields": {
            "brands": "$brands.name",
            "categories": "$categories.name"
        } }
    ]);
    

    这也与

    db.business.aggregate([
        { "$lookup": {
            "from": "categories",
            "localField": "categories",
            "foreignField": "_id",
            "as": "categories"
        } },
        { "$lookup": {
            "from": "brands",
            "localField": "brands",
            "foreignField": "_id",
            "as": "brands"
        } },
        { "$addFields": {
            "brands": {
                "$map": {
                    "input": "$brands",
                    "as": "brand",
                    "in": "$$brand.name"
                } 
            },
            "categories": {
                "$map": {
                    "input": "$categories",
                    "as": "cat",
                    "in": "$$cat.name"
                } 
            }  
        } }
    ]);