Read Data from MongoDB
On this page
Overview
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.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the Ruby driver installed in your Ruby project.
Copy the following code and paste it into a new
.rb
file.Copy a code example from this page and paste it on the specified lines in the file.
1 require 'bundler/inline' 2 3 gemfile do 4 source 'https://rubygems.org' 5 gem 'mongo' 6 end 7 8 uri = '<connection string>' 9 10 Mongo::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 17 end
Find One
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.
Find Multiple
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.
Count Documents in a Collection
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.
Count Documents Returned from a Query
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.
Estimated Document Count
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.
Retrieve Distinct Values
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.
Monitor Data Changes
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.