如何在MongoDB聚合中包含外部数组?

时间:2016-12-09 23:22:20

标签: mongodb mongoose aggregation-framework

在聚合项目数组元素中,从索引x到y,其中x在集合内定义,y是与匹配的数组元素的索引对应的数组元素。请参阅下面的示例,然后我将更容易理解我想说的内容。

优惠券收藏

{"coupon_id": "coupon01", "codes": ["FLAT30", "FLAT50", "FLAT70", "FLAT90"], "curr_index": 0}

例如,请参阅下面的示例代码,我在尝试获取从curr_index到(curr_index + n)的优惠券代码,其中n是coupons_ctrs中与coupons_ids索引对应的数字示例 - 对于id "584559bd1f65d363bd5d25fd" n是1,对于id "58455a5c1f65d363bd5d2600" n为2,对于id "584878eb0005202c64b0cc5d" n为3。

coupons_ctrs = [1, 2, 3];
coupons_ids = ["584559bd1f65d363bd5d25fd", "58455a5c1f65d363bd5d2600", "584878eb0005202c64b0cc5d"];

int n = 2;
couponmodel.aggregate(
        { $match : { '_id': { $in : coupons_ids }} },
        { $project: {_id:0, codes : /* How to use slice here so that codes array will be returned between cur_index and (curr_index + coupons_ctr corresponding to the index coupon id is found, example- for _id "584559bd1f65d363bd5d25fd" it should be 1 and so on) */} },
        function(err, docs) {
            if (err) {

            } else {

            }
        });

更新

正如Styvane所建议的那样,我可以使用$ zip,它可以完美运行,但因为我使用的是mongoDB 3.2.11所以我无法使用它,所以在mongodb 3.2中使用$ zip功能的解决方案是什么。 11 ??

任何人都可以告诉我如何包含此coupon_ctrs数组并在聚合管道中使用它。

1 个答案:

答案 0 :(得分:1)

您需要使用$slice运算符将$map表达式应用于“coupons_ctrs”数组中的每个元素,这意味着我们使用文字“coupons_ctrs”数组作为{{1的“输入” }}

$map

哪个收益率:

let coupons_ids = [
    "584559bd1f65d363bd5d25fd", 
    "58455a5c1f65d363bd5d2600", 
    "584878eb0005202c64b0cc5d"
];

let coupons_ctrs = [1, 2, 3];

db.couponmodel.aggregate(
    [
        { "$match" : { "_id": { "$in" : coupons_ids } } },
        { "$project": { 
            "codes":  { 
                "$map": { 
                    "input": coupons_ctrs, 
                    "as": "n", 
                    "in": { 
                        "$slice": [ 
                            "$codes", 
                            "$curr_index", 
                            { "$add": [ "$curr_index", "$$n" ] } 
                        ]
                    }
                }
            }
        }}
    ]
)

在MongoDB 3.4中,我们可以使用$zip运算符来执行此操作:

{
    "codes" : [
        [ "FLAT30" ],
        [ "FLAT30", "FLAT50" ],
        [ "FLAT30", "FLAT50",  "FLAT70" ]
    ]
}

返回这样的内容:

db.couponmodel.aggregate(     
    [ 
        { "$project": {
            "codes":  {
                "$map": {
                    "input": { 
                        "$zip": { 
                            "inputs": [ coupons_ids, coupons_ctrs ] 
                        } 
                    }, 
                    "as": "item",
                    "in": {  
                        "coupon_id": { "$arrayElemAt": [ "$$item", 0 ] },
                        "value": {          
                            "$slice": [  
                                "$codes",       
                                "$curr_index",    
                                { "$add": [ 
                                    "$curr_index", 
                                    { "$arrayElemAt": [ "$$item", 1 ] } 
                                ] }                          
                            ]
                         }
                    }     
                } 
            } 
        }}     
    ] 
)