开发者

Batch call to save "new" objects in mongoid

开发者 https://www.devze.com 2023-03-11 04:52 出处:网络
A large array of objects gets initialized (Photo.new). Some filtering is done to the Photo array, so 30% of them remains.
  1. A large array of objects gets initialized (Photo.new).
  2. Some filtering is done to the Photo array, so 30% of them remains.
  3. The remaining array gets saved in one single call.

Questions

  1. What is the command to do step 3, which is essentially a batch save?
  2. Is there a limit to how many ob开发者_运维技巧jects I can save at once?


I'm not familiar with the Ruby Mongoid driver, but it seems like you're looking for something like:

photos.map &:save

If you are trying to do this in a less iterative fashion (i.e. in a single call) it appears that the Ruby Mongoid driver supports an insert method to save an array of hashes:

Photo.collection.insert(photos)

Source: Batch insert/update using Mongoid?


Yes, there is a limit. Everything you're going to batch insert using the method coreyward just described should be within 16M (MongoDB's document size limit). Look here:

https://github.com/mongodb/mongo-ruby-driver/commit/4712a684689c11a31221c87354e5ae0864960226

So you should estimate your array's byte size and split it into several parts if it exceeds 16M.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号