Finding mongoDB records in batches (using mongoid ruby adapter)

后端 未结 6 1954
悲哀的现实
悲哀的现实 2020-12-14 00:32

Using rails 3 and mongoDB with the mongoid adapter, how can I batch finds to the mongo DB? I need to grab all the records in a particular mongo DB collection and index them

6条回答
  •  星月不相逢
    2020-12-14 00:50

    With Mongoid, you don't need to manually batch the query.

    In Mongoid, Model.all returns a Mongoid::Criteria instance. Upon calling #each on this Criteria, a Mongo driver cursor is instantiated and used to iterate over the records. This underlying Mongo driver cursor already batches all records. By default the batch_size is 100.

    For more information on this topic, read this comment from the Mongoid author and maintainer.

    In summary, you can just do this:

    Model.all.each do |r|
      Sunspot.index(r)
    end
    

提交回复
热议问题