Class: Moped::Query
- Inherits:
-
Object
- Object
- Moped::Query
- Includes:
- Enumerable, Retryable
- Defined in:
- lib/moped/query.rb
Overview
The Query
class encapsulates all of the logic related to building selectors for querying, updating, or removing documents in a collection.
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
- #collection The collection to execute the query on.(Thecollectiontoexecutethequeryon.) ⇒ Object readonly
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
- #operation The query operation.(Thequeryoperation.) ⇒ Object readonly
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
- #selector The query selector.(Thequeryselector.) ⇒ Object readonly
Instance Method Summary collapse
-
#batch_size(batch_size) ⇒ Query
Set the query’s batch size.
- #cluster ⇒ Object
-
#count(limit = false) ⇒ Integer
Get the count of matching documents in the query.
-
#cursor ⇒ Moped::Cursor
Get the Moped cursor to iterate over documents on the db.
-
#distinct(key) ⇒ Array<Object ] The distinct values.
Get the distinct values in the collection for the provided key.
-
#each(*args) {|document| ... } ⇒ Enumerable
Iterate through documents matching the query’s selector.
-
#explain ⇒ Hash
Explain the current query.
-
#first ⇒ Hash
(also: #one)
Get the first matching document.
-
#hint(hint) ⇒ Query
Apply an index hint to the query.
-
#initialize(collection, selector) ⇒ Query
constructor
Initialize the query.
-
#limit(limit) ⇒ Query
Set the query’s limit.
-
#max(index_bounds) ⇒ Query
Specifies the exclusive upper bound for a specific index in order to constrain the results of find().
-
#max_scan(max) ⇒ Query
Apply a max scan limit to the query.
-
#min(index_bounds) ⇒ Query
Specify the inclusive lower bound for a specific index in order to constrain the results of find().
-
#modify(change, options = {}) ⇒ Hash
Execute a $findAndModify on the query.
-
#no_timeout ⇒ Query
Disable cursor timeout.
- #query_options(options) ⇒ Object
- #read_preference ⇒ Object
-
#remove ⇒ Hash?
Remove a single document matching the query’s selector.
-
#remove_all ⇒ Hash?
Remove multiple documents matching the query’s selector.
-
#select(select) ⇒ Query
Set the fields to include or exclude from the query.
-
#skip(skip) ⇒ Query
Set the number of documents to skip.
-
#sort(sort) ⇒ Query
Set the sort order for the query.
-
#tailable ⇒ Query
Tell the query to create a tailable cursor.
-
#update(change, flags = nil) ⇒ Hash?
Update a single document matching the query’s selector.
-
#update_all(change) ⇒ Hash?
Update multiple documents matching the query’s selector.
-
#upsert(change) ⇒ Hash?
Update an existing document with
change
, otherwise create one. - #write_concern ⇒ Object
Constructor Details
#initialize(collection, selector) ⇒ Query
Initialize the query.
222 223 224 225 226 227 228 229 |
# File 'lib/moped/query.rb', line 222 def initialize(collection, selector) @collection, @selector = collection, selector @operation = Protocol::Query.new( collection.database.name, collection.name, selector ) end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
29 30 31 |
# File 'lib/moped/query.rb', line 29 def collection @collection end |
#collection The collection to execute the query on.(Thecollectiontoexecutethequeryon.) ⇒ Object (readonly)
29 |
# File 'lib/moped/query.rb', line 29 attr_reader :collection, :operation, :selector |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
29 30 31 |
# File 'lib/moped/query.rb', line 29 def operation @operation end |
#operation The query operation.(Thequeryoperation.) ⇒ Object (readonly)
29 |
# File 'lib/moped/query.rb', line 29 attr_reader :collection, :operation, :selector |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
29 30 31 |
# File 'lib/moped/query.rb', line 29 def selector @selector end |
#selector The query selector.(Thequeryselector.) ⇒ Object (readonly)
29 |
# File 'lib/moped/query.rb', line 29 attr_reader :collection, :operation, :selector |
Instance Method Details
#batch_size(batch_size) ⇒ Query
Set the query’s batch size.
256 257 258 259 |
# File 'lib/moped/query.rb', line 256 def batch_size(batch_size) operation.batch_size = batch_size self end |
#cluster ⇒ Object
485 486 487 |
# File 'lib/moped/query.rb', line 485 def cluster session.cluster end |
#count(limit = false) ⇒ Integer
Get the count of matching documents in the query.
39 40 41 42 43 44 |
# File 'lib/moped/query.rb', line 39 def count(limit = false) command = { count: collection.name, query: selector } command.merge!(skip: operation.skip, limit: operation.limit) if limit result = collection.database.command(command) result["n"].to_i end |
#cursor ⇒ Moped::Cursor
Get the Moped cursor to iterate over documents on the db.
92 93 94 |
# File 'lib/moped/query.rb', line 92 def cursor Cursor.new(session, operation) end |
#distinct(key) ⇒ Array<Object ] The distinct values.
Get the distinct values in the collection for the provided key.
56 57 58 59 60 61 62 63 |
# File 'lib/moped/query.rb', line 56 def distinct(key) result = collection.database.command( distinct: collection.name, key: key.to_s, query: selector ) result["values"] end |
#each(*args) {|document| ... } ⇒ Enumerable
Iterate through documents matching the query’s selector.
77 78 79 |
# File 'lib/moped/query.rb', line 77 def each(*args, &blk) cursor.each(*args, &blk) end |
#explain ⇒ Hash
Explain the current query.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/moped/query.rb', line 104 def explain explanation = operation.selector.dup hint = explanation["$hint"] sort = explanation["$orderby"] max_scan = explanation["$maxScan"] explanation = { "$query" => selector, "$explain" => true, } explanation["$orderby"] = sort if sort explanation["$hint"] = hint if hint explanation["$maxScan"] = max_scan if max_scan Query.new(collection, explanation).limit(-(operation.limit.abs)).each { |doc| return doc } end |
#first ⇒ Hash Also known as: one
Get the first matching document.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/moped/query.rb', line 127 def first reply = read_preference.with_node(cluster) do |node| node.query( operation.database, operation.collection, operation.selector, ( fields: operation.fields, flags: operation.flags, skip: operation.skip, limit: -1 ) ) end reply.documents.first end |
#hint(hint) ⇒ Query
Apply an index hint to the query.
155 156 157 158 159 |
# File 'lib/moped/query.rb', line 155 def hint(hint) upgrade_to_advanced_selector operation.selector["$hint"] = hint self end |
#limit(limit) ⇒ Query
Set the query’s limit.
241 242 243 244 |
# File 'lib/moped/query.rb', line 241 def limit(limit) operation.limit = limit self end |
#max(index_bounds) ⇒ Query
Specifies the exclusive upper bound for a specific index in order to constrain the results of find(). max() provides a way to specify an upper bound on compound key indexes.
(provided the collection has a => -11 index)
db[:people].find.min("age" => 21)
207 208 209 210 211 |
# File 'lib/moped/query.rb', line 207 def max(index_bounds) upgrade_to_advanced_selector operation.selector["$max"] = index_bounds self end |
#max_scan(max) ⇒ Query
Apply a max scan limit to the query.
171 172 173 174 175 |
# File 'lib/moped/query.rb', line 171 def max_scan(max) upgrade_to_advanced_selector operation.selector["$maxScan"] = max self end |
#min(index_bounds) ⇒ Query
Specify the inclusive lower bound for a specific index in order to constrain the results of find(). min() provides a way to specify lower bounds on compound key indexes.
(provided the collection has a => 1 index)
db[:people].find.min("age" => 21)
189 190 191 192 193 |
# File 'lib/moped/query.rb', line 189 def min(index_bounds) upgrade_to_advanced_selector operation.selector["$min"] = index_bounds self end |
#modify(change, options = {}) ⇒ Hash
Execute a $findAndModify on the query.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/moped/query.rb', line 298 def modify(change, = {}) command = { findAndModify: collection.name, query: selector }.merge() command[:sort] = operation.selector["$orderby"] if operation.selector["$orderby"] command[:fields] = operation.fields if operation.fields command[:update] = change unless [:remove] result = session.with(read: :primary) do |sess| sess.command(command)["value"] end # Keeping moped compatibility with mongodb >= 2.2.0-rc0 [:upsert] && !result ? {} : result end |
#no_timeout ⇒ Query
Disable cursor timeout
269 270 271 272 |
# File 'lib/moped/query.rb', line 269 def no_timeout operation.no_timeout = true self end |
#query_options(options) ⇒ Object
489 490 491 |
# File 'lib/moped/query.rb', line 489 def () read_preference.() end |
#read_preference ⇒ Object
481 482 483 |
# File 'lib/moped/query.rb', line 481 def read_preference session.read_preference end |
#remove ⇒ Hash?
Remove a single document matching the query’s selector.
324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/moped/query.rb', line 324 def remove with_retry(cluster) do cluster.with_primary do |node| node.remove( operation.database, operation.collection, operation.basic_selector, write_concern, flags: [ :remove_first ] ) end end end |
#remove_all ⇒ Hash?
Remove multiple documents matching the query’s selector.
346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/moped/query.rb', line 346 def remove_all with_retry(cluster) do cluster.with_primary do |node| node.remove( operation.database, operation.collection, operation.basic_selector, write_concern ) end end end |
#select(select) ⇒ Query
Set the fields to include or exclude from the query.
369 370 371 372 |
# File 'lib/moped/query.rb', line 369 def select(select) operation.fields = select self end |
#skip(skip) ⇒ Query
Set the number of documents to skip.
384 385 386 387 |
# File 'lib/moped/query.rb', line 384 def skip(skip) operation.skip = skip self end |
#sort(sort) ⇒ Query
Set the sort order for the query.
399 400 401 402 403 |
# File 'lib/moped/query.rb', line 399 def sort(sort) upgrade_to_advanced_selector operation.selector["$orderby"] = sort self end |
#tailable ⇒ Query
Tell the query to create a tailable cursor.
413 414 415 416 |
# File 'lib/moped/query.rb', line 413 def tailable operation.flags.push(:tailable, :await_data) self end |
#update(change, flags = nil) ⇒ Hash?
Update a single document matching the query’s selector.
430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/moped/query.rb', line 430 def update(change, flags = nil) with_retry(cluster) do cluster.with_primary do |node| node.update( operation.database, operation.collection, operation.selector["$query"] || operation.selector, change, write_concern, flags: flags ) end end end |
#update_all(change) ⇒ Hash?
Update multiple documents matching the query’s selector.
455 456 457 |
# File 'lib/moped/query.rb', line 455 def update_all(change) update(change, [ :multi ]) end |
#upsert(change) ⇒ Hash?
Update an existing document with change
, otherwise create one.
473 474 475 |
# File 'lib/moped/query.rb', line 473 def upsert(change) update(change, [ :upsert ]) end |
#write_concern ⇒ Object
477 478 479 |
# File 'lib/moped/query.rb', line 477 def write_concern session.write_concern end |