diff --git a/README.md b/README.md index 0695a79..b5219bc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/ruby/hello-world/.gitignore b/ruby/hello-world/.gitignore new file mode 100644 index 0000000..72bcafc --- /dev/null +++ b/ruby/hello-world/.gitignore @@ -0,0 +1,3 @@ +.bundle/ +vendor/bundle/ +Gemfile.lock diff --git a/ruby/hello-world/Gemfile b/ruby/hello-world/Gemfile new file mode 100644 index 0000000..ffb6904 --- /dev/null +++ b/ruby/hello-world/Gemfile @@ -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' diff --git a/ruby/hello-world/README.md b/ruby/hello-world/README.md new file mode 100644 index 0000000..9e116ed --- /dev/null +++ b/ruby/hello-world/README.md @@ -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 +`` with your connection string: + +```bash +export MONGODB_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. diff --git a/ruby/hello-world/hello_world.rb b/ruby/hello-world/hello_world.rb new file mode 100644 index 0000000..a6a2452 --- /dev/null +++ b/ruby/hello-world/hello_world.rb @@ -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