【问题标题】:update in mongodb with $in query?使用 $in 查询在 mongodb 中更新?
【发布时间】:2017-04-29 18:19:19
【问题描述】:

我有三个数组 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 与聚合一起使用,那么我的问题将很容易解决,但由于聚合不支持 $update,我该如何解决我的问题。

【问题讨论】:

  • 一次更新一张优惠券。
  • 是的,但它会增加我服务器上的时间,这就是为什么要考虑任何其他解决方案??为什么不能在单个查询中做到这一点??
  • 我怀疑。所有bulk update 都发生在客户端。没有往返开销。磁盘 IO 的数量取决于要更新的​​文档数量,而不是请求数量。您能否详细说明为什么您认为时间会增加?
  • 我的意思是并行运行 10 个查询,您不认为在一个查询中更新它们会更好吗??
  • @AlexBlex 你能看看下面的问题吗,我遇到了你建议的批量更新问题。请回复。stackoverflow.com/questions/41163644/…

标签: mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多