【问题标题】:How to include external array inside MongoDB aggregation?如何在 MongoDB 聚合中包含外部数组?
【发布时间】:2017-04-25 12:29:52
【问题描述】:

在聚合项目中从索引 x 到 y 的数组元素,其中 x 在集合内部定义,y 是与匹配的数组元素的索引对应的数组元素。请看下面的例子,这样就更容易理解我想说的了。

优惠券收藏

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

例如,请参见下面的示例代码,我正在尝试获取从 curr_index 到 (curr_index + n) 的优惠券代码,其中 n 是 coupon_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 数组并在聚合管道中使用它。

【问题讨论】:

    标签: mongodb mongoose aggregation-framework


    【解决方案1】:

    您需要使用$map 运算符将$slice 表达式应用于“coupons_ctrs”数组中的每个元素,这意味着我们使用文字“coupons_ctrs”数组作为$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" ] } 
                            ]
                        }
                    }
                }
            }}
        ]
    )
    

    产量:

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

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

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

    返回如下内容:

    {
        "codes" : [
            {
                 "coupon_id" : "584559bd1f65d363bd5d25fd",
                 "value" : [ "FLAT30" ]
            },
            {
                 "coupon_id" : "58455a5c1f65d363bd5d2600",
                 "value" : [ "FLAT30", "FLAT50" ],
            },
            {
                 "coupon_id" : "584878eb0005202c64b0cc5d",
                 "value" : [ "FLAT30", "FLAT50",  "FLAT70" ]
            }
        ]
    }
    

    【讨论】:

    • 在您的代码中定义了 id: "584559bd1f65d363bd5d25fd" n 将是 1,我的意思是是否有任何保证,对于每个匹配 "n" 的 id 将是相同的索引元素“优惠券_ctr”。 ??
    • @PrakashKumar 是的,它会,但您需要提供“coupons_ids”元素索引需要与您显示的“coupons_ctrs”元素的顺序相同。
    • 是的,它是按 smae 顺序排列的,但是这段代码怎么能在内部做到这一点,你能解释一下吗??
    • @PrakashKumar 你运行代码了吗?效果是否如您所愿?
    • 对不起,但不是第一个 id 我得到三个代码 [["GETFREE"],["GETFREE"],["GETFREE"]],第二个 id 我得到 [["FET50" ],["FET50"],["FET50"]] 第三个我得到 [["versace"],["versace","versace"],["versace","versace"]]。跨度>
    猜你喜欢
    • 2021-12-16
    • 2020-03-29
    • 1970-01-01
    • 2017-10-07
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2019-09-28
    相关资源
    最近更新 更多