class Mongo::Cursor

Overview

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.

Included Modules

Direct Known Subclasses

Defined in:

cryomongo/cursor.cr

Instance Method Summary

Instance Method Detail

def close #

Close the cursor and frees underlying resources.


def finalize #

Clean up the underlying resource when garbage collected.


def next #
Description copied from module Iterator(BSON)

Returns the next element in this iterator, or Iterator::Stop::INSTANCE if there are no more elements.


def 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.