使用 $in 查询在 mongodb 中更新? [英] update in mongodb with $in query?

查看:23
本文介绍了使用 $in 查询在 mongodb 中更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have three arrays coupon_ids, id_counter and increase_ctr. Now the value of coupon_id at any index is present at same index in id_counter and in increase_ctr. So for coupon_id "584559bd1f65d363bd5d25f0" id_counter value is 200 and increase_ctr is 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].

Now can anyone please tell me how can i write above query so that for coupon_id[index] i can use "id_counter" = id_counter[index] and "increase_ctr" = increase_ctr[index] in the query.

如果我们可以将 $update 与聚合一起使用,那么我的问题将很容易解决,但由于聚合不支持 $update,我该如何解决我的问题.

If we could use $update with aggregate then my problem will get solved easily but as Aggregates does not support $update how can i solve my problem.

推荐答案

您不能根据具有不同更新的不同查询更新多个文档,但是更新单独的文档应该非常快,因为您不会等待先前的更新发生在文档上.

You cannot update multiple documents based on diffrent queries with diffrent updates, However updating the seperate documents should be pretty fast as you won't be waiting for the previous update to have happend on the document.

这篇关于使用 $in 查询在 mongodb 中更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆