Mongo::Cursor
Inherits Iterator / Enumerable / Reference / Object
A Cursor is a pointer to the result set of a query.
This class implements the Iterator module under the hood.
# Find is one of the methods that return a cursor.
cursor = collection.find({qty: {"$gt": 20}})
# Using `to_a` iterates the cursor until the end and stores the elements inside an `Array`.
elements = cursor.to_a
# `to_a` is one of the methods inherited from the `Iterator` module.
Instance methods
next
Returns the next element in this iterator, or Iterator::Stop::INSTANCE if there
are no more elements.
of(type : T) forall T
Will convert the elements to the T type while iterating the Cursor.
Assumes that T has a constructor method named from_bson that takes a single BSON argument.
# Using .of is shorter than…
wrapped_cursor = cursor.of(Type)
# …having to .map and initialize.
wrapped_cursor = cursor.map { |element| Type.from_bson(element) }
NOTE: Internally, wraps the cursor inside a Mongo::Cursor::Wrapper with type T.