Class: Mongo::Protocol::Query

Inherits:
Message
  • Object
show all
Defined in:
lib/mongo/protocol/query.rb

Overview

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.

Defined Under Namespace

Classes: Upconverter

Constant Summary

Constants inherited from Message

Message::BATCH_SIZE, Message::COLLECTION, Message::LIMIT, Message::MAX_MESSAGE_SIZE, Message::ORDERED, Message::Q

Instance Attribute Summary

Attributes inherited from Message

#request_id

Instance Method Summary collapse

Methods inherited from Message

#==, deserialize, #hash, #serialize, #set_request_id

Constructor Details

#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 slave ok bit set

Query.new('xgen', 'users', {:name => 'Tyler'}, :flags => [:slave_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):

  • :project (Hash)

    The projection.

  • :skip (Integer)

    The number of documents to skip.

  • :limit (Integer)

    The number of documents to return.

  • :flags (Array)

    The flags for the query message.

    Supported flags: :tailable_cursor, :slave_ok, :oplog_replay, :no_cursor_timeout, :await_data, :exhaust, :partial



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongo/protocol/query.rb', line 60

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

Instance Method Details

#payloadHash

Return the event payload for monitoring.

Examples:

Return the event payload.

message.payload

Returns:

  • (Hash)

    The event payload.

Since:

  • 2.1.0



81
82
83
84
85
86
87
88
# File 'lib/mongo/protocol/query.rb', line 81

def payload
  {
    command_name: upconverter.command_name,
    database_name: @database,
    command: upconverter.command,
    request_id: request_id
  }
end

#replyable?true

Query messages require replies from the database.

Examples:

Does the message require a reply?

message.replyable?

Returns:

  • (true)

    Always true for queries.

Since:

  • 2.0.0



98
99
100
# File 'lib/mongo/protocol/query.rb', line 98

def replyable?
  true
end