Class: Moped::Query
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.
221 222 223 224 225 226 227 228 |
# File 'lib/moped/query.rb', line 221 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.
28 29 30 |
# File 'lib/moped/query.rb', line 28 def collection @collection end |
#collection The collection to execute the query on.(Thecollectiontoexecutethequeryon.) ⇒ Object (readonly)
28 |
# File 'lib/moped/query.rb', line 28 attr_reader :collection, :operation, :selector |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
28 29 30 |
# File 'lib/moped/query.rb', line 28 def operation @operation end |
#operation The query operation.(Thequeryoperation.) ⇒ Object (readonly)
28 |
# File 'lib/moped/query.rb', line 28 attr_reader :collection, :operation, :selector |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
28 29 30 |
# File 'lib/moped/query.rb', line 28 def selector @selector end |
#selector The query selector.(Thequeryselector.) ⇒ Object (readonly)
28 |
# File 'lib/moped/query.rb', line 28 attr_reader :collection, :operation, :selector |
Instance Method Details
#batch_size(batch_size) ⇒ Query
Set the query’s batch size.
255 256 257 258 |
# File 'lib/moped/query.rb', line 255 def batch_size(batch_size) operation.batch_size = batch_size self end |
#cluster ⇒ Object
478 479 480 |
# File 'lib/moped/query.rb', line 478 def cluster session.cluster end |
#count(limit = false) ⇒ Integer
Get the count of matching documents in the query.
38 39 40 41 42 43 |
# File 'lib/moped/query.rb', line 38 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.
91 92 93 |
# File 'lib/moped/query.rb', line 91 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.
55 56 57 58 59 60 61 62 |
# File 'lib/moped/query.rb', line 55 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.
76 77 78 |
# File 'lib/moped/query.rb', line 76 def each(*args, &blk) cursor.each(*args, &blk) end |
#explain ⇒ Hash
Explain the current query.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/moped/query.rb', line 103 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/moped/query.rb', line 126 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.
154 155 156 157 158 |
# File 'lib/moped/query.rb', line 154 def hint(hint) upgrade_to_advanced_selector operation.selector["$hint"] = hint self end |
#limit(limit) ⇒ Query
Set the query’s limit.
240 241 242 243 |
# File 'lib/moped/query.rb', line 240 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)
206 207 208 209 210 |
# File 'lib/moped/query.rb', line 206 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.
170 171 172 173 174 |
# File 'lib/moped/query.rb', line 170 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)
188 189 190 191 192 |
# File 'lib/moped/query.rb', line 188 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.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/moped/query.rb', line 297 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
268 269 270 271 |
# File 'lib/moped/query.rb', line 268 def no_timeout operation.no_timeout = true self end |
#query_options(options) ⇒ Object
482 483 484 |
# File 'lib/moped/query.rb', line 482 def () read_preference.() end |
#read_preference ⇒ Object
474 475 476 |
# File 'lib/moped/query.rb', line 474 def read_preference session.read_preference end |
#remove ⇒ Hash?
Remove a single document matching the query’s selector.
323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/moped/query.rb', line 323 def remove cluster.with_primary do |node| node.remove( operation.database, operation.collection, operation.basic_selector, write_concern, flags: [ :remove_first ] ) end end |
#remove_all ⇒ Hash?
Remove multiple documents matching the query’s selector.
343 344 345 346 347 348 349 350 351 352 |
# File 'lib/moped/query.rb', line 343 def remove_all cluster.with_primary do |node| node.remove( operation.database, operation.collection, operation.basic_selector, write_concern ) end end |
#select(select) ⇒ Query
Set the fields to include or exclude from the query.
364 365 366 367 |
# File 'lib/moped/query.rb', line 364 def select(select) operation.fields = select self end |
#skip(skip) ⇒ Query
Set the number of documents to skip.
379 380 381 382 |
# File 'lib/moped/query.rb', line 379 def skip(skip) operation.skip = skip self end |
#sort(sort) ⇒ Query
Set the sort order for the query.
394 395 396 397 398 |
# File 'lib/moped/query.rb', line 394 def sort(sort) upgrade_to_advanced_selector operation.selector["$orderby"] = sort self end |
#tailable ⇒ Query
Tell the query to create a tailable cursor.
408 409 410 411 |
# File 'lib/moped/query.rb', line 408 def tailable operation.flags.push(:tailable, :await_data) self end |
#update(change, flags = nil) ⇒ Hash?
Update a single document matching the query’s selector.
425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/moped/query.rb', line 425 def update(change, flags = nil) cluster.with_primary do |node| node.update( operation.database, operation.collection, operation.selector["$query"] || operation.selector, change, write_concern, flags: flags ) end end |
#update_all(change) ⇒ Hash?
Update multiple documents matching the query’s selector.
448 449 450 |
# File 'lib/moped/query.rb', line 448 def update_all(change) update(change, [ :multi ]) end |
#upsert(change) ⇒ Hash?
Update an existing document with change
, otherwise create one.
466 467 468 |
# File 'lib/moped/query.rb', line 466 def upsert(change) update(change, [ :upsert ]) end |
#write_concern ⇒ Object
470 471 472 |
# File 'lib/moped/query.rb', line 470 def write_concern session.write_concern end |