A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/ruby-driver/current/read/specify-a-query/ below:

Specify a Query - Ruby Driver

In this guide, you can learn how to specify a query by using the Ruby driver.

You can refine the set of documents that a query returns by creating a query filter. A query filter is an expression that specifies the search criteria that MongoDB uses to match documents in a read or write operation. In a query filter, you can prompt the driver to search for documents that have an exact match to your query, or you can compose query filters to express more complex matching criteria.

The examples in this guide run operations on the fruits collection, which contains documents representing fruits. The following code example shows how to create a database and collection, and then insert the sample documents into your collection:

database = client.use('db')collection = database[:fruits]fruits = [{ _id: 1, name: 'apples', qty: 5, rating: 3, color: 'red', type: ['fuji', 'honeycrisp'] },{ _id: 2, name: 'bananas', qty: 7, rating: 4, color: 'yellow', type: ['cavendish'] },{ _id: 3, name: 'oranges', qty: 6, rating: 2, type: ['naval', 'mandarin'] },{ _id: 4, name: 'pineapples', qty: 3, rating: 5, color: 'yellow' }]collection.insert_many(fruits)

Literal value queries return documents that have an exact match to your query filter.

The following example specifies a query filter as a parameter to the find method. The code returns all documents in which the value of the color field is 'yellow':

filter = { color: 'yellow' }results = collection.find(filter)results.each do |doc|  puts docend
{"_id"=>2, "name"=>"bananas", "qty"=>7, "rating"=>4, "color"=>"yellow", "type"=>["cavendish"]}{"_id"=>4, "name"=>"pineapples", "qty"=>3, "rating"=>5, "color"=>"yellow"}
Note Find All Documents

To find all documents in a collection, call the find method without passing any parameters:

results = collection.find

Comparison operators evaluate a document field value against a specified value in your query filter. The following list describes common comparison operators:

The following example specifies a comparison operator in a query filter as a parameter to the find method. The code returns all documents that have a rating field value greater than 2:

filter = { rating: { '$gt' => 2 } }results = collection.find(filter)results.each do |doc|  puts docend
{"_id"=>1, "name"=>"apples", "qty"=>5, "rating"=>3, "color"=>"red", "type"=>["fuji", "honeycrisp"]}{"_id"=>2, "name"=>"bananas", "qty"=>7, "rating"=>4, "color"=>"yellow", "type"=>["cavendish"]}{"_id"=>4, "name"=>"pineapples", "qty"=>3, "rating"=>5, "color"=>"yellow"}

Logical operators match documents by using logic applied to the results of two or more sets of expressions. The following list describes each logical operator:

The following example specifies a logical operator in a query filter as a parameter to the find method. The code returns all documents that have a qty field value greater than 5 or a color field value of 'yellow':

filter = { '$or' => [{ qty: { '$gt' => 5 } }, { color: 'yellow' }] }results = collection.find(filter)results.each do |doc|  puts docend
{"_id"=>2, "name"=>"bananas", "qty"=>7, "rating"=>4, "color"=>"yellow", "type"=>["cavendish"]}{"_id"=>3, "name"=>"oranges", "qty"=>6, "rating"=>2, "type"=>["naval", "mandarin"]}{"_id"=>4, "name"=>"pineapples", "qty"=>3, "rating"=>5, "color"=>"yellow"}

Array operators match documents based on the value or quantity of elements in an array field. The following list describes each array operator:

Tip

To learn more about array operators, see the Array Query Operators guide in the MongoDB Server manual.

The following example specifies an array operator in a query filter as a parameter to the find method. The code returns all documents in which the type array field contains 2 elements:

filter = { type: { '$size' => 2 } }results = collection.find(filter)results.each do |doc|  puts docend
{"_id"=>1, "name"=>"apples", "qty"=>5, "rating"=>3, "color"=>"red", "type"=>["fuji", "honeycrisp"]}{"_id"=>3, "name"=>"oranges", "qty"=>6, "rating"=>2, "type"=>["naval", "mandarin"]}

Element operators query data based on the presence or type of a field. The following list describes each element operator:

The following example specifies an element operator in a query filter as a parameter to the find method. The code returns all documents that have a color field:

filter = { color: { '$exists' => true } }results = collection.find(filter)results.each do |doc|  puts docend
{"_id"=>1, "name"=>"apples", "qty"=>5, "rating"=>3, "color"=>"red", "type"=>["fuji", "honeycrisp"]}{"_id"=>2, "name"=>"bananas", "qty"=>7, "rating"=>4, "color"=>"yellow", "type"=>["cavendish"]}{"_id"=>4, "name"=>"pineapples", "qty"=>3, "rating"=>5, "color"=>"yellow"}

Evaluation operators return data based on evaluations of either individual fields or the entire collection's documents. The following list describes common element operators:

The following example specifies an evaluation operator in a query filter as a parameter to the find method. The code uses a regular expression to return all documents in which the name field value has at least two consecutive 'p' characters:

filter = { name: /p{2,}/ }results = collection.find(filter)results.each do |doc|  puts docend
{"_id"=>1, "name"=>"apples", "qty"=>5, "rating"=>3, "color"=>"red", "type"=>["fuji", "honeycrisp"]}{"_id"=>4, "name"=>"pineapples", "qty"=>3, "rating"=>5, "color"=>"yellow"}
Note

The Ruby driver implicitly uses the $regex operator when a query filter includes a regular expression value, as shown in the preceding example.

To learn more about querying documents, see Query Documents in the MongoDB Server manual.

To learn more about retrieving documents by using the Ruby driver, see the Retrieve Data guide.

To learn more about the find method, see the API documentation.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4