Docs Menu
Docs Home
/ / /
Ruby MongoDB Driver

Read Data from MongoDB

On this page

  • Overview
  • Sample Application
  • Find One
  • Find Multiple
  • Count Documents in a Collection
  • Count Documents Returned from a Query
  • Estimated Document Count
  • Retrieve Distinct Values
  • Monitor Data Changes

On this page, you can see copyable code examples that show common Ruby driver methods you can use to read data from MongoDB.

Tip

To learn more about any of the methods shown on this page, see the link provided in each section.

To use an example from this page, copy the code example into the Sample Application below or your own application. Be sure to replace all placeholders in the code examples, such as <connection string URI>, with the relevant values for your MongoDB deployment.

You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:

  1. Ensure you have the Ruby driver installed in your Ruby project.

  2. Copy the following code and paste it into a new .rb file.

  3. Copy a code example from this page and paste it on the specified lines in the file.

1require 'bundler/inline'
2
3gemfile do
4 source 'https://rubygems.org'
5 gem 'mongo'
6end
7
8uri = '<connection string>'
9
10Mongo::Client.new(uri) do |client|
11 database = client.use('<database name>')
12 collection = database['<collection name>']
13
14 # Start example code here
15
16 # End example code here
17end

The following example retrieves a document that matches the criteria specified by the given filter:

document = collection.find(name: '<value>').first
puts document

To learn more about the first method, see the Retrieve Data guide.

The following example retrieves all documents that match the criteria specified by the given filter:

results = collection.find(founded_year: '<value>')

To learn more about the find method, see the Retrieve Data guide.

The following example returns the number of documents in the specified collection:

result = collection.count_documents
puts "Number of documents: #{result}"

To learn more about the count_documents method, see the Count Documents guide.

The following example returns the number of documents in the specified collection that match the query criteria:

result = collection.count_documents('key': '<value>')
puts "value: #{result}"

To learn more about the countDocuments() method, see the Count Documents guide.

The following example returns an approximate number of documents in the specified collection based on collection metadata:

result = collection.estimated_document_count
puts "Estimated number of documents: #{result}"

To learn more about the estimated_document_count() method, see the Count Documents guide.

The following example returns all distinct values of the specified field name in a given collection:

results = collection.distinct('field')

To learn more about the distinct method, see the Retrieve Distinct Field Values guide.

The following example creates a change stream for a given collection and prints out subsequent change events in that collection:

stream = collection.watch
collection.insert_one(a: 1)
doc = stream.first
process(doc)

To learn more about the watch() method, see the Monitor Data Changes guide.

Back

Run a Command