class Mongo::Protocol::Query

MongoDB Wire protocol Query message.

This is a client request message that is sent to the server in order to retrieve documents matching provided query.

Users may also provide additional options such as a projection, to select a subset of the fields, a number to skip or a limit on the number of returned documents.

There are a variety of flags that can be used to adjust cursor parameters or the desired consistency and integrity the results.

@api semipublic

Constants

FLAGS

Available flags for a Query message.

OP_CODE

The operation code required to specify a Query message. @return [Fixnum] the operation code.

@since 2.5.0

Attributes

upconverter[R]

Public Class Methods

new(database, collection, selector, options = {}) click to toggle source

Creates a new Query message

@example Find all users named Tyler.

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

@example Find all users named Tyler skipping 5 and returning 10.

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

@example Find all users with slave ok bit set

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

@example Find all user ids.

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

@param database [String, Symbol] The database to query. @param collection [String, Symbol] The collection to query. @param selector [Hash] The query selector. @param options [Hash] The additional query options.

@option options :project [Hash] The projection. @option options :skip [Integer] The number of documents to skip. @option options :limit [Integer] The number of documents to return. @option options :flags [Array] The flags for the query message.

Supported flags: +:tailable_cursor+, +:slave_ok+, +:oplog_replay+,
+:no_cursor_timeout+, +:await_data+, +:exhaust+, +:partial+
Calls superclass method
# File lib/mongo/protocol/query.rb, line 61
def initialize(database, collection, selector, options = {})
  @database = database
  @namespace = "#{database}.#{collection}"
  @selector = selector
  @options = options
  @project = options[:project]
  @limit = determine_limit
  @skip = options[:skip]  || 0
  @flags = options[:flags] || []
  @upconverter = Upconverter.new(collection, selector, options, flags)
  super
end

Public Instance Methods

compress!(compressor, zlib_compression_level = nil) click to toggle source

Compress this message.

@param [ String, Symbol ] compressor The compressor to use. @param [ Integer ] zlib_compression_level The zlib compression level to use.

@return [ Compressed, self ] A Protocol::Compressed message or self, depending on whether

this message can be compressed.

@since 2.5.0

# File lib/mongo/protocol/query.rb, line 112
def compress!(compressor, zlib_compression_level = nil)
  if compressor && compression_allowed?(selector.keys.first)
    Compressed.new(self, compressor, zlib_compression_level)
  else
    self
  end
end
payload() click to toggle source

Return the event payload for monitoring.

@example Return the event payload.

message.payload

@return [ BSON::Document ] The event payload.

@since 2.1.0

# File lib/mongo/protocol/query.rb, line 82
def payload
  BSON::Document.new(
    command_name: upconverter.command_name,
    database_name: @database,
    command: upconverter.command,
    request_id: request_id
  )
end
replyable?() click to toggle source

Query messages require replies from the database.

@example Does the message require a reply?

message.replyable?

@return [ true ] Always true for queries.

@since 2.0.0

# File lib/mongo/protocol/query.rb, line 99
def replyable?
  true
end

Private Instance Methods

determine_limit() click to toggle source
# File lib/mongo/protocol/query.rb, line 132
def determine_limit
  [ @options[:limit] || @options[:batch_size], @options[:batch_size] || @options[:limit] ].min || 0
end