Class: Mongoid::Criteria

Inherits:
Object
  • Object
show all
Defined in:
lib/theusual/mongoid.rb

Instance Method Summary collapse

Instance Method Details

#as_docObject Also known as: as_docs



8
9
10
# File 'lib/theusual/mongoid.rb', line 8

def as_doc
  to_a.map &:as_doc
end

#find_in_batches(opts = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/theusual/mongoid.rb', line 20

def find_in_batches(opts = {}, &block)
  batch_size = opts[:batch_size] || 1000
  offset = opts[:start] || 0

  loop do
    docs = skip(offset).limit(batch_size).to_a

    if docs.empty?
      break
    else
      yield docs
      offset += batch_size
    end
  end
end

#last(n = 1) ⇒ Object

can’t seem to override Mongoid::Document::last, so put this functionality here



15
16
17
# File 'lib/theusual/mongoid.rb', line 15

def last(n = 1)
  order(_id: -1).limit(n)
end