Class: Moped::Protocol::Query

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

Overview

The Protocol class for querying a collection.

Since:

  • 1.0.0

Direct Known Subclasses

Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Message

included, #inspect, #serialize

Constructor Details

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

Instantiate a new query operation.

Examples:

Find all users named John.

Query.new("moped", "users", { name: "John" })

Find all users named John skipping 5 and returning 10.

Query.new("moped", "users", { name: "John" }, skip: 5, limit: 10)

Find all users on slave node.

Query.new("moped", "users", {}, flags: [ :slave_ok ])

Find all user ids.

Query.new("moped", "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):

  • :request_id (Integer)

    The operation’s request id.

  • :skip (Integer)

    The number of documents to skip.

  • :limit (Integer)

    The number of documents to return.

  • :fields (Hash)

    The limited fields to return.

  • :flags (Array)

    The flags for querying. Supported flags are: :tailable, :slave_ok, :no_cursor_timeout, :await_data, :exhaust.

Since:

  • 1.0.0



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/moped/protocol/query.rb', line 151

def initialize(database, collection, selector, options = {})
  @database = database
  @collection = collection
  @full_collection_name = "#{database}.#{collection}"
  @selector = selector
  @request_id = options[:request_id]
  @flags = options[:flags] || []
  @limit = options[:limit]
  @skip = options[:skip]
  @fields = options[:fields]
  @batch_size = options[:batch_size]
end

Instance Attribute Details

#batch_sizeInteger

Returns The batch size of the results.

Returns:

  • (Integer)

    The batch size of the results.



66
67
68
# File 'lib/moped/protocol/query.rb', line 66

def batch_size
  @batch_size
end

#collectionString

Returns The collection to query.

Returns:

  • (String)

    The collection to query.



62
63
64
# File 'lib/moped/protocol/query.rb', line 62

def collection
  @collection
end

#databaseObject

Since:

  • 1.0.0



62
# File 'lib/moped/protocol/query.rb', line 62

attr_reader :collection, :database

#fieldsHash?

Returns the fields to include in the reply.

Returns:

  • (Hash, nil)

    the fields to include in the reply



54
# File 'lib/moped/protocol/query.rb', line 54

document :fields, :optional => true

#flagsArray

The flags for the query. Supported flags are: :tailable, :slave_ok, :no_cursor_timeout, :await_data, :exhaust.

Parameters:

  • flags (Array)

    the flags for this message

Returns:

  • (Array)

    the flags for this message



30
31
32
33
34
# File 'lib/moped/protocol/query.rb', line 30

flags :flags, tailable:          2 ** 1,
slave_ok:          2 ** 2,
no_cursor_timeout: 2 ** 4,
await_data:        2 ** 5,
exhaust:           2 ** 6

#full_collection_nameString

Returns the namespaced collection name.

Returns:

  • (String)

    the namespaced collection name



38
# File 'lib/moped/protocol/query.rb', line 38

cstring :full_collection_name

#lengthInteger

Returns the length of the message.

Returns:

  • (Integer)

    the length of the message



12
# File 'lib/moped/protocol/query.rb', line 12

int32 :length

#limitInteger

Returns the number of documents to return.

Returns:

  • (Integer)

    the number of documents to return



46
# File 'lib/moped/protocol/query.rb', line 46

int32 :limit

#op_codeInteger

Get the code for a query operation.

Examples:

Get the operation code.

query.op_code

Returns:

  • (Integer)

    OP_QUERY operation code (2004).

Since:

  • 1.0.0



22
# File 'lib/moped/protocol/query.rb', line 22

int32 :op_code

#request_idInteger

Returns the request id of the message.

Returns:

  • (Integer)

    the request id of the message



16
# File 'lib/moped/protocol/query.rb', line 16

int32 :request_id

#selectorHash

Returns the selector for this query.

Returns:

  • (Hash)

    the selector for this query



50
# File 'lib/moped/protocol/query.rb', line 50

document :selector

#skipInteger

Returns the number of documents to skip.

Returns:

  • (Integer)

    the number of documents to skip



42
# File 'lib/moped/protocol/query.rb', line 42

int32 :skip

Instance Method Details

#basic_selectorHash

Note:

Sometimes, like in cases of deletion we need this since MongoDB does not understand $query in operations like DELETE.

Get the basic selector.

Examples:

Get the basic selector.

query.basic_selector

Returns:

  • (Hash)

    The basic selector.

Since:

  • 2.0.0



79
80
81
# File 'lib/moped/protocol/query.rb', line 79

def basic_selector
  selector["$query"] || selector
end

#failure?(reply) ⇒ true, false

Determine if the provided reply message is a failure with respect to a query.

Examples:

Is the reply a query failure?

query.failure?(reply)

Parameters:

  • reply (Reply)

    The reply to the query.

Returns:

  • (true, false)

    If the reply is a failure.

Since:

  • 2.0.0



108
109
110
# File 'lib/moped/protocol/query.rb', line 108

def failure?(reply)
  reply.query_failure?
end

#failure_exception(reply) ⇒ Moped::Errors::QueryFailure

Get the exception specific to a failure of this particular operation.

Examples:

Get the failure exception.

query.failure_exception(document)

Parameters:

Returns:

Since:

  • 2.0.0



93
94
95
# File 'lib/moped/protocol/query.rb', line 93

def failure_exception(reply)
  Errors::QueryFailure.new(self, reply.documents.first)
end

#log_inspectString

Provide the value that will be logged when the query runs.

Examples:

Provide the log inspection.

query.log_inspect

Returns:

  • (String)

    The string value for logging.

Since:

  • 1.0.0



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/moped/protocol/query.rb', line 172

def log_inspect
  type = "QUERY"
  fields = []
  fields << ["%-12s", type]
  fields << ["database=%s", database]
  fields << ["collection=%s", collection]
  fields << ["selector=%s", selector.inspect]
  fields << ["flags=%s", flags.inspect]
  fields << ["limit=%s", limit.inspect]
  fields << ["skip=%s", skip.inspect]
  fields << ["batch_size=%s", batch_size.inspect]
  fields << ["fields=%s", self.fields.inspect]
  f, v = fields.transpose
  f.join(" ") % v
end

#no_timeout=(enable) ⇒ Object

Set the option on the query to not timeout the cursor.

Examples:

Set the no timeout option.

query.no_timeout = true

Parameters:

  • enable (true, false)

    Whether to enable the no timeout option.

Since:

  • 1.3.0



120
121
122
# File 'lib/moped/protocol/query.rb', line 120

def no_timeout=(enable)
  @flags |= [:no_cursor_timeout] if enable
end

#receive_replies(connection) ⇒ Protocol::Reply

Receive replies to the message.

Examples:

Receive replies.

message.receive_replies(connection)

Parameters:

Returns:

Since:

  • 1.0.0



212
213
214
# File 'lib/moped/protocol/query.rb', line 212

def receive_replies(connection)
  connection.read
end

#results(reply) ⇒ Moped::Protocol::Reply

Take the provided reply and return the expected results to api consumers.

Examples:

Get the expected results of the reply.

query.results(reply)

Parameters:

Returns:

Since:

  • 2.0.0



227
228
229
# File 'lib/moped/protocol/query.rb', line 227

def results(reply)
  reply
end