Navigate to your quickstart.rb
file in the ruby-quickstart
directory. Copy and paste the following code below the Bundler code from the Download and Install step of this tutorial. This code connects to MongoDB and queries the movies
collection in the sample_mflix
database.
uri = '<connection string>'begin client = Mongo::Client.new(uri) database = client.use('sample_mflix') movies = database[:movies] query = { title: 'Back to the Future' } movie = movies.find(query).first puts movieensure client&.closeend
Replace the <connection string>
placeholder with the connection string that you copied from the Create a Connection String step of this tutorial.
From your ruby-quickstart
directory, run the following shell command to run the application:
The command line output contains details about the retrieved movie document:
{"_id"=>BSON::ObjectId('...'), "plot"=>"A young man is accidentally sent30 years into the past in a time-traveling DeLorean invented by his friend,Dr. Emmett Brown, and must make sure his high-school-age parents unitein order to save his own existence.", ..."title"=>"Back to the Future", ...
If you encounter an error or see no output, ensure that you specified the correct connection string in the quickstart.rb
file and that you loaded the sample data.
After you complete these steps, you have a working application that uses the driver to connect to your MongoDB deployment, runs a query on the sample data, and prints out the result.
NoteIf you run into issues on this step, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right side of this page.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4