Class | Moped::Query |
In: |
lib/moped/query.rb
|
Parent: | Object |
The Query class encapsulates all of the logic related to building selectors for querying, updating, or removing documents in a collection.
@example
people = db[:people] people.find.entries # => [{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}] people.find.skip(2).first # => { id: 3 } people.find.skip(2).update(name: "John") people.find.skip(2).first # => { id: 3, name: "John" } people.find(name: nil).update_all("$set" => { name: "Unknown" }) people.find.one # => { id: 5, name: "Unknown" } people.find.first # => { id: 5, name: "Unknown" } people.find.select(name: 0).first # => { id: 5 } people.find(name: "Unknown").remove_all people.find.count # => 1
collection | [R] | @attribute [r] collection The collection to execute the query on. @attribute [r] operation The query operation. @attribute [r] selector The query selector. |
operation | [R] | @attribute [r] collection The collection to execute the query on. @attribute [r] operation The query operation. @attribute [r] selector The query selector. |
selector | [R] | @attribute [r] collection The collection to execute the query on. @attribute [r] operation The query operation. @attribute [r] selector The query selector. |
Initialize the query.
@example Initialize the query.
Query.new(collection, selector)
@param [ Collection ] collection The query‘s collection. @param [ Hash ] selector The query‘s selector.
@since 1.0.0
Get the Moped cursor to iterate over documents on the db.
@example Iterate over the matching documents.
db[:people].cursor.each do |doc| #... end
@return [ Moped::Cursor ]
@since 2.0.0
Iterate through documents matching the query‘s selector.
@example Iterate over the matching documents.
db[:people].find.each do |doc| #... end
@return [ Enumerable ]
@since 1.0.0
@yieldparam [ Hash ] document each matching document
Explain the current query.
@example Explain the query.
db[:people].find.explain
@return [ Hash ] The explain document.
@since 1.0.0
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.
@example Set the upper bond on the age index to 21 years (provided the collection has a {"age" => -11} index)
db[:people].find.min("age" => 21)
@param [ Hash ] indexBounds The exclusive upper bound for the index keys.
@return [ Query ] self
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.
@example Set the lower bond on the age index to 21 years (provided the collection has a {"age" => 1} index)
db[:people].find.min("age" => 21)
@param [ Hash ] indexBounds The inclusive lower bound for the index keys.
@return [ Query ] self
Execute a $findAndModify on the query.
@example Find and modify a document, returning the original.
db[:bands].find.modify({ "$inc" => { likes: 1 }})
@example Find and modify a document, returning the updated document.
db[:bands].find.modify({ "$inc" => { likes: 1 }}, new: true)
@example Find and return a document, removing it from the database.
db[:bands].find.modify({}, remove: true)
@example Find and return a document, upserting if no match found.
db[:bands].find.modify({}, upsert: true, new: true)
@param [ Hash ] change The changes to make to the document. @param [ Hash ] options The options.
@option options :new Set to true if you want to return the updated document. @option options :remove Set to true if the document should be deleted. @option options :upsert Set to true if you want to upsert
@return [ Hash ] The document.
@since 1.0.0
Remove a single document matching the query‘s selector.
@example Remove a single document.
db[:people].find(name: "John").remove
@return [ Hash, nil ] If in safe mode the last error result.
@since 1.0.0
Remove multiple documents matching the query‘s selector.
@example Remove all matching documents.
db[:people].find(name: "John").remove_all
@return [ Hash, nil ] If in safe mode the last error result.
@since 1.0.0
Update a single document matching the query‘s selector.
@example Update the first matching document.
db[:people].find(_id: 1).update(name: "John")
@param [ Hash ] change The changes to make to the document @param [ Array ] flags An array of operation flags. Valid values are:
+:multi+ and +:upsert+
@return [ Hash, nil ] If in safe mode the last error result.
@since 1.0.0
Update multiple documents matching the query‘s selector.
@example Update multiple documents.
db[:people].find(name: "John").update_all("$set" => { name: "Mary" })
@param [ Hash ] change The changes to make to the documents
@return [ Hash, nil ] If in safe mode the last error result.
@since 1.0.0
Update an existing document with change, otherwise create one.
@example Upsert the changes.
db[:people].find.entries # => { name: "John" } db[:people].find(name: "John").upsert(name: "James") db[:people].find.entries # => { name: "James" } db[:people].find(name: "John").upsert(name: "Mary") db[:people].find.entries # => [{ name: "James" }, { name: "Mary" }]
@param [ Hash ] change The changes to make to the the document.
@return [ Hash, nil ] If in safe mode the last error result.
@since 1.0.0