用$ in查询在mongodb中更新?

时间:2016-12-14 12:08:27

标签: mongodb mongoose mongodb-query aggregation-framework

我有三个数组coupon_ids,id_counter和increase_ctr。现在,任何索引处的coupon_id值都存在于id_counter和increase_ctr中的相同索引处。所以对于coupon_id" 584559bd1f65d363bd5d25f0" id_counter值为200,increase_ctr为5。

coupon_ids = ["584559bd1f65d363bd5d25f0", "584559bd1f65d363bd5d25f1", "584559bd1f65d363bd5d25f2", "584559bd1f65d363bd5d25f4"];

id_counter = [200, 300, 400];

increase_ctr = [5, 6, 7];

couponmodel.findAndUpdate({'_id' : { $in : coupons_ids }, "id_counter": id_counter[index] /* how to get id_counter corresponding to index which is matched inside $in */}, { id_counter: new_id_counter, $inc: {curr_ctr: increase_ctr /* how to get increate_ctr corresponding to index which is matched inside $in */} }, function(err, numberAffected, raw){
    if(err){

    } else {

    }
});

现在任何人都可以告诉我如何写上面的查询,以便coupon_id [index]我可以使用" id_counter" = id_counter [index]和" increase_ctr" =查询中的increase_ctr [index]。

如果我们可以使用$ update with aggregate,那么我的问题将很容易解决,但由于Aggregates不支持$ update,我如何解决我的问题。

1 个答案:

答案 0 :(得分:0)

您无法根据具有不同更新的不同查询来更新多个文档。但是,更新单独的文档应该非常快,因为您不会等待上一次更新发生在文档上。