Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ includes its own setup instructions.
- [Node.js](node/hello-world/README.md)
- PHP
- [Python](python/hello-world/README.md)
- Ruby
- [Ruby](ruby/hello-world/README.md)
- Rust
- Scala

Expand Down
3 changes: 3 additions & 0 deletions ruby/hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.bundle/
vendor/bundle/
Gemfile.lock
6 changes: 6 additions & 0 deletions ruby/hello-world/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'

gem 'mongo', '~> 2.0'

# Required by bson on Ruby 3.4+, where bigdecimal is no longer a default gem.
gem 'bigdecimal'
62 changes: 62 additions & 0 deletions ruby/hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Get Started with the MongoDB Ruby Driver

This sample application connects to a MongoDB deployment, seeds a small
set of sample product documents, and retrieves one of them. Because the
app inserts its own data, you don't need to load an external dataset.

## Prerequisites

Before you begin, complete the [Atlas Get Started guide](https://www.mongodb.com/docs/get-started/)
to create a free Atlas deployment and save your database user
credentials.

You also need the following components installed in your development environment:

- Ruby version 3.3 or later
- Bundler

The Ruby driver is not officially supported on Windows.

## Installation

Clone this repository:

```bash
git clone https://github.com/mongodb/mongodb-code-examples
```

Navigate into the `ruby/hello-world` project directory and install the
`mongo` gem with Bundler:

```bash
cd mongodb-code-examples/ruby/hello-world
bundle install
```

## Connect to MongoDB

Set your connection string as an environment variable, replacing
`<connection string uri>` with your connection string:

```bash
export MONGODB_URI="<connection string uri>"
```

## Run the Application

```bash
bundle exec ruby hello_world.rb
```

When you run the app, it inserts a few product documents into the
`get_started.products` collection, then queries and prints one of them:

```
{"_id":{"$oid":"..."},"name":"Wireless Mouse","category":"Electronics","price":24.99,"tags":["wireless","usb","ergonomic"]}
```

You can run the app more than once. It clears the collection before
each run, so the results stay consistent.

If you encounter an error or see no output, verify that you set the
`MONGODB_URI` environment variable correctly.
50 changes: 50 additions & 0 deletions ruby/hello-world/hello_world.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'mongo'
require 'json'

# Keep driver logging quiet so the app prints only the query result.
Mongo::Logger.logger.level = Logger::WARN

uri = ENV['MONGODB_URI']
if uri.nil? || uri.empty?
warn 'Set the MONGODB_URI environment variable to your connection string'
exit 1
end

# A few sample product documents seeded by this app so you can run
# it without loading an external dataset.
SAMPLE_PRODUCTS = [
{
name: 'Wireless Mouse',
category: 'Electronics',
price: 24.99,
tags: %w[wireless usb ergonomic]
},
{
name: 'Standing Desk',
category: 'Furniture',
price: 349.99,
tags: %w[adjustable office]
},
{
name: 'Noise-Cancelling Headphones',
category: 'Electronics',
price: 199.99,
tags: %w[bluetooth wireless over-ear]
}
].freeze

client = Mongo::Client.new(uri, database: 'get_started')

begin
products = client[:products]

# Seed the collection so the app has data to query. Clearing the
# collection first keeps results consistent across repeated runs.
products.delete_many({})
products.insert_many(SAMPLE_PRODUCTS)

product = products.find(name: 'Wireless Mouse').first
puts JSON.generate(product.as_extended_json(mode: :relaxed))
ensure
client.close
end