Module: Poro::Contexts::MongoContext::FinderMethods

Included in:
Poro::Contexts::MongoContext
Defined in:
lib/poro/contexts/mongo_context.rb

Overview

A mixin of MongoDB finder method implementations.

Instance Method Summary collapse

Instance Method Details

#data_store_cursor(opts) ⇒ Object

Runs the given find parameters on MongoDB and returns a Mongo::Cursor object. Note that you must manually convert the results using this Context’s convert_to_plain_object(obj) method or you will get raw Mongo objects.

If a block is given, the cursor is automatically iterated over via the each method, but with the results pre-converterd. Note that the result set can change out from under you on an active system if you iterate in this way. Additionally, the returned cursor has been rewound, which means it may find different results!

This method is useful if you need to do something special, like only get one result at a time to save on memory.

WARNING: Even though the method currently does no filtering of the conditions, allowing advanced queries will work, in the future this may not be the case. If your query needs to do more than a simple query, it is better to use data_store_find_all.



514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/poro/contexts/mongo_context.rb', line 514

def data_store_cursor(opts) # :yields: plain_object
  find_opts = mongoize_find_opts(opts)
  cursor = data_store.find(opts[:conditions], find_opts)
  
  if( block_given? )
    cursor.each do |doc|
      plain_object = self.convert_to_plain_object(doc)
      yield(plain_object)
    end
    cursor.rewind!
  end
  
  return cursor
end