Batch call to save "new" objects in mongoid
- A large array of objects gets initialized (
Photo.new
). - Some filtering is done to the Photo array, so 30% of them remains.
- The remaining array gets saved in one single call.
Questions
- What is the command to do step 3, which is essentially a batch save?
- 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.
精彩评论