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
-
#count ⇒ Integer
Get the count of matching documents in the query.
-
#distinct(key) ⇒ Array<Object ] The distinct values.
Get the distinct values in the collection for the provided key.
-
#each {|document| ... } ⇒ Enumerator
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.
-
#modify(change, options = {}) ⇒ Hash
Execute a $findAndModify on the query.
-
#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.
-
#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.
Constructor Details
#initialize(collection, selector) ⇒ Query
Initialize the query.
147 148 149 150 151 152 153 154 |
# File 'lib/moped/query.rb', line 147 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.
25 26 27 |
# File 'lib/moped/query.rb', line 25 def collection @collection end |
#collection The collection to execute the query on.(Thecollectiontoexecutethequeryon.) ⇒ Object (readonly)
25 |
# File 'lib/moped/query.rb', line 25 attr_reader :collection, :operation, :selector |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
25 26 27 |
# File 'lib/moped/query.rb', line 25 def operation @operation end |
#operation The query operation.(Thequeryoperation.) ⇒ Object (readonly)
25 |
# File 'lib/moped/query.rb', line 25 attr_reader :collection, :operation, :selector |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
25 26 27 |
# File 'lib/moped/query.rb', line 25 def selector @selector end |
#selector The query selector.(Thequeryselector.) ⇒ Object (readonly)
25 |
# File 'lib/moped/query.rb', line 25 attr_reader :collection, :operation, :selector |
Instance Method Details
#count ⇒ Integer
Get the count of matching documents in the query.
35 36 37 38 39 40 41 |
# File 'lib/moped/query.rb', line 35 def count result = collection.database.command( count: collection.name, query: selector ) result["n"].to_i end |
#distinct(key) ⇒ Array<Object ] The distinct values.
Get the distinct values in the collection for the provided key.
53 54 55 56 57 58 59 60 |
# File 'lib/moped/query.rb', line 53 def distinct(key) result = collection.database.command( distinct: collection.name, key: key.to_s, query: selector ) result["values"] end |
#each {|document| ... } ⇒ Enumerator
Iterate through documents matching the query’s selector.
74 75 76 77 78 79 80 81 |
# File 'lib/moped/query.rb', line 74 def each cursor = Cursor.new(session, operation) enum = cursor.to_enum enum.each do |document| yield document end if block_given? enum end |
#explain ⇒ Hash
Explain the current query.
91 92 93 94 95 96 97 98 |
# File 'lib/moped/query.rb', line 91 def explain operation.selector = { "$query" => selector, "$orderby" => operation.selector.fetch("$orderby", {}), "$explain" => true, "$limit" => operation.selector.fetch("$limit", 1).abs * -1 } and each.next end |
#first ⇒ Hash Also known as: one
Get the first matching document.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/moped/query.rb', line 108 def first reply = session.context.query( operation.database, operation.collection, operation.selector, fields: operation.fields, flags: operation.flags, skip: operation.skip, limit: -1 ) reply.documents.first end |
#hint(hint) ⇒ Query
Apply an index hint to the query.
132 133 134 135 136 |
# File 'lib/moped/query.rb', line 132 def hint(hint) operation.selector = { "$query" => selector } unless operation.selector["$query"] operation.selector["$hint"] = hint self end |
#limit(limit) ⇒ Query
Set the query’s limit.
166 167 168 169 |
# File 'lib/moped/query.rb', line 166 def limit(limit) operation.limit = limit self end |
#modify(change, options = {}) ⇒ Hash
Execute a $findAndModify on the query.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/moped/query.rb', line 195 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] session.with(consistency: :strong) do |sess| sess.command(command)["value"] end end |
#remove ⇒ Hash?
Remove a single document matching the query’s selector.
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/moped/query.rb', line 218 def remove session.with(consistency: :strong) do |session| session.context.remove( operation.database, operation.collection, operation.selector, flags: [ :remove_first ] ) end end |
#remove_all ⇒ Hash?
Remove multiple documents matching the query’s selector.
237 238 239 240 241 242 243 244 245 |
# File 'lib/moped/query.rb', line 237 def remove_all session.with(consistency: :strong) do |session| session.context.remove( operation.database, operation.collection, operation.selector ) end end |
#select(select) ⇒ Query
Set the fields to include or exclude from the query.
257 258 259 260 |
# File 'lib/moped/query.rb', line 257 def select(select) operation.fields = select self end |
#skip(skip) ⇒ Query
Set the number of documents to skip.
272 273 274 275 |
# File 'lib/moped/query.rb', line 272 def skip(skip) operation.skip = skip self end |
#sort(sort) ⇒ Query
Set the sort order for the query.
287 288 289 290 |
# File 'lib/moped/query.rb', line 287 def sort(sort) operation.selector = { "$query" => selector, "$orderby" => sort } self end |
#update(change, flags = nil) ⇒ Hash?
Update a single document matching the query’s selector.
304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/moped/query.rb', line 304 def update(change, flags = nil) session.with(consistency: :strong) do |session| session.context.update( operation.database, operation.collection, operation.selector, change, flags: flags ) end end |
#update_all(change) ⇒ Hash?
Update multiple documents matching the query’s selector.
326 327 328 |
# File 'lib/moped/query.rb', line 326 def update_all(change) update(change, [ :multi ]) end |
#upsert(change) ⇒ Hash?
Update an existing document with change
, otherwise create one.
344 345 346 |
# File 'lib/moped/query.rb', line 344 def upsert(change) update(change, [ :upsert ]) end |