Read

Finding documents is done in the same way as creating them, at the collection level:

@collection.find( { isbn: '101' } )

Multiple search criteria can be chained and are equivalent to an AND operator in SQL:

@collection.find( { isbn: '101', name: 'Mastering MongoDB' } )

The mongo-ruby-driver provides several query options to enhance queries, the most widely used of which are listed in this table:

Option

Description

allow_partial_results

This is for use with sharded clusters. If a shard is down, it allows the query to return results from the shards that are up, potentially only getting a portion of the results.

batch_size(Integer)

Can change the batch size that the cursor will fetch from MongoDB. This is done on each GETMORE operation (for example, typing it on the mongo shell)

comment(String)

With this command we can add a comment in our query for documentation reasons.

hint(Hash)

We can force usage of an index using hint().

limit(Integer)

We can limit the result set to the number of documents specified by Integer.

max_scan(Integer)

We can limit the number of documents that will be scanned. This will return incomplete results and is useful if we are performing operations that we want to guarantee that they won't take a long time, such as, for example, if we connect to our production database.

no_cursor_timeout

If we don't specify this parameter, MongoDB will close any inactive cursor after 600 seconds. With this parameter our cursor will never be closed.

projection(Hash)

We can use this parameter to fetch or exclude specific attributes from our results. Will reduce data transfer over the wire. An example would be:

client[:books].find.projection(:price => 1)

read(Hash)

We can specify a read preference to be applied only for this query:

client[:books].find.read(:mode => :secondary_preferred)

show_disk_loc(Boolean)

We should use this option if for some reason we want to find the actual location of our results on disk

skip(Integer)

To skip the specified number of documents. Useful for pagination of results.

snapshot

To execute our query in snapshot mode. This is useful when we want more stringent consistency.

sort(Hash)

We can use this to sort our results. For example:

client[:books].find.sort(:name => -1)

On top of the query options, mongo-ruby-driver provides some helper functions that can be chained at the method call level:

  • .count: Total count for the preceding query
  • .distinct(:field_name): Distinguish the results of the preceding query by :field_name

Find() returns a cursor containing the result set, which we can iterate using .each in Ruby like every other object:

result = @collection.find({ isbn: '101' })
result.each do |doc|
puts doc.inspect
end

The output is as follows for our books collection:

{"_id"=>BSON::ObjectId('592149c4aabac953a3a1e31e'), "isbn"=>"101", "name"=>"Mastering MongoDB", "price"=>30.0, "published"=>2017-06-25 00:00:00 UTC}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.213.27