Method: Mongo::Protocol::Query#initialize

Defined in:
lib/mongo/protocol/query.rb

#initialize(database, collection, selector, options = {}) ⇒ Query

Creates a new Query message

Examples:

Find all users named Tyler.

Query.new('xgen', 'users', {:name => 'Tyler'})

Find all users named Tyler skipping 5 and returning 10.

Query.new('xgen', 'users', {:name => 'Tyler'}, :skip => 5,
                                               :limit => 10)

Find all users with secondaryOk bit set

Query.new('xgen', 'users', {:name => 'Tyler'}, :flags => [:secondary_ok])

Find all user ids.

Query.new('xgen', 'users', {}, :fields => {:id => 1})

Parameters:

  • database (String, Symbol)

    The database to query.

  • collection (String, Symbol)

    The collection to query.

  • selector (Hash)

    The query selector.

  • options (Hash) (defaults to: {})

    The additional query options.

Options Hash (options):

  • :flags (Array<Symbol>)

    The flag bits. Currently supported values are :await_data, :exhaust, :no_cursor_timeout, :oplog_replay, :partial, :secondary_ok, :tailable_cursor.

  • :limit (Integer)

    The number of documents to return.

  • :project (Hash)

    The projection.

  • :skip (Integer)

    The number of documents to skip.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mongo/protocol/query.rb', line 64

def initialize(database, collection, selector, options = {})
  @database = database
  @namespace = "#{database}.#{collection}"
  if selector.nil?
    raise ArgumentError, 'Selector cannot be nil'
  end
  @selector = selector
  @options = options
  @project = options[:project]
  @limit = determine_limit
  @skip = options[:skip]  || 0
  @flags = options[:flags] || []
  @upconverter = Upconverter.new(
    collection,
    BSON::Document.new(selector),
    BSON::Document.new(options),
    flags,
  )
  super
end