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.

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 }

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

Create a new query command.

Examples:

Query.new "moped", "users", { name: "John" },
  skip: 5,
  limit: 10,
  request_id: 12930,
  fields: { _id: -1, name: 1 }

Parameters:

  • database (String, Symbol)

    the database to insert into

  • collection (String, Symbol)

    the collection to insert into

  • selector (Hash)

    the query

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

    additional options

Options Hash (options):

  • :request_id (Number)

    the command’s request id

  • :skip (Number)

    the number of documents to skip

  • :limit (Number)

    the number of documents to return

  • :fields (Hash)

    the fields to return

  • :flags (Array)

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



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/moped/protocol/query.rb', line 107

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_sizeObject

Returns the value of attribute batch_size.



81
82
83
# File 'lib/moped/protocol/query.rb', line 81

def batch_size
  @batch_size
end

#collectionString, Symbol (readonly)

Returns the collection to query.

Returns:



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

def collection
  @collection
end

#databaseString, Symbol (readonly)

Returns the database to query.

Returns:



76
77
78
# File 'lib/moped/protocol/query.rb', line 76

def database
  @database
end

#fieldsHash?

Returns the fields to include in the reply.

Returns:

  • (Hash, nil)

    the fields to include in the reply



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

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



41
42
43
44
45
# File 'lib/moped/protocol/query.rb', line 41

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



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

cstring  :full_collection_name

#lengthNumber

Returns the length of the message.

Returns:

  • (Number)

    the length of the message



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

int32 :length

#limitNumber

Returns the number of documents to return.

Returns:

  • (Number)

    the number of documents to return



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

int32    :limit

#op_codeNumber

Returns OP_QUERY operation code (2004).

Returns:

  • (Number)

    OP_QUERY operation code (2004)



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

int32 :op_code

#request_idNumber

Returns the request id of the message.

Returns:

  • (Number)

    the request id of the message



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

int32 :request_id

#selectorHash

Returns the selector for this query.

Returns:

  • (Hash)

    the selector for this query



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

document :selector

#skipNumber

Returns the number of documents to skip.

Returns:

  • (Number)

    the number of documents to skip



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

int32    :skip

Instance Method Details

#basic_selectorHash

Note:

Sometimes, like in cases of deletion we need this since MongoDB

Get the basic selector.

does not understand $query in operations like DELETE.

Examples:

Get the basic selector.

query.basic_selector

Returns:

  • (Hash)

    The basic selector.

Since:

  • 2.0.0



148
149
150
# File 'lib/moped/protocol/query.rb', line 148

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

#log_inspectObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/moped/protocol/query.rb', line 121

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



83
84
85
# File 'lib/moped/protocol/query.rb', line 83

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



162
163
164
# File 'lib/moped/protocol/query.rb', line 162

def receive_replies(connection)
  connection.read
end