2016-12-14 92 views
0

我有三個數組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 [索引]我可以用「id_counter」 = id_counter [ index]和「increase_ctr」= increase_ctr [index]。

如果我們可以使用$ update與聚合然後我的問題將得到輕鬆解決,但作爲聚合不支持$更新我該如何解決我的問題。

+0

一次更新1個優惠券。 –

+0

是的,但它會增加我的服務器上的時間,這就是爲什麼思考任何其他解決方案?爲什麼不能在單個查詢中做到這一點? –

+0

我很懷疑。所有[批量更新](https://docs.mongodb.com/v3.2/reference/method/Bulk.find.update/)都發生在客戶端。沒有往返的開銷。磁盤IO的數量取決於要更新的​​文檔數量,而不取決於請求數量。你能否詳細說明爲什麼你認爲時間會增加? –

回答

0

不能更新基於不同更新的不同更新的多個文檔,但更新單獨的文檔應該非常快,因爲您不會等待以前的更新發生在文檔上。