mongo-ruby-driver/docs/TUTORIAL.md

357 lines
13 KiB
Markdown
Raw Permalink Normal View History

2010-11-03 21:50:08 +00:00
# MongoDB Ruby Driver Tutorial
2010-11-03 16:41:56 +00:00
This tutorial gives many common examples of using MongoDB with the Ruby driver. If you're looking for information on data modeling, see [MongoDB Data Modeling and Rails](http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails). Links to the various object mappers are listed on our [object mappers page](http://www.mongodb.org/display/DOCS/Object+Mappers+for+Ruby+and+MongoDB).
2010-11-29 22:04:39 +00:00
Interested in GridFS? See [GridFS in Ruby](file.GridFS.html).
2010-11-03 16:41:56 +00:00
As always, the [latest source for the Ruby driver](http://github.com/mongodb/mongo-ruby-driver) can be found on [github](http://github.com/mongodb/mongo-ruby-driver/).
## Installation
The mongo-ruby-driver gem is served through Rubygems.org. To install, make sure you have the latest version of rubygems.
2010-11-03 16:41:56 +00:00
gem update --system
2010-11-03 16:41:56 +00:00
Next, install the mongo rubygem:
2010-11-03 16:41:56 +00:00
gem install mongo
The required `bson` gem will be installed automatically.
For optimum performance, install the bson\_ext gem:
2010-11-03 16:41:56 +00:00
gem install bson_ext
After installing, you may want to look at the [examples](http://github.com/mongodb/mongo-ruby-driver/tree/master/examples) directory included in the source distribution. These examples walk through some of the basics of using the Ruby driver.
## Getting started
Note that the output in the following has been updated to Ruby 1.9, so if you are using Ruby 1.8, you will see some minor differences. To follow this tutorial interactively, at the command line, run the Interactive Ruby Shell.
irb
As you execute commands, irb will output the result using the `inspect` method. If you are editing and running a script for this tutorial, you can view output using the `puts` or `p` methods.
### Using the gem
2010-11-03 16:41:56 +00:00
Use the `mongo` gem via the `require` kernel method.
2010-11-03 16:41:56 +00:00
require 'rubygems' # not necessary for Ruby 1.9
require 'mongo'
### Making a Connection
2010-11-03 16:41:56 +00:00
An `Mongo::Connection` instance represents a connection to MongoDB. You can optionally specify the MongoDB server address and port when connecting. The following example shows three ways to connect to the local machine:
2010-11-03 16:41:56 +00:00
connection = Mongo::Connection.new # (optional host/port args)
connection = Mongo::Connection.new("localhost")
connection = Mongo::Connection.new("localhost", 27017)
2010-11-03 16:41:56 +00:00
### Listing All Databases
2010-11-03 16:41:56 +00:00
connection.database_names
connection.database_info.each { |info| puts info.inspect }
2010-11-03 16:41:56 +00:00
The `database_info` method returns a hash mapping database names to the size of the database in bytes.
2010-11-03 16:41:56 +00:00
## Using a Database
You use a Connection instance to obtain an Mongo::DB instance, which represents a named database. The database doesn't have to exist - if it doesn't, MongoDB will create it for you. The following examples use the database "mydb":
db = connection.db("mydb")
db = Mongo::Connection.new.db("mydb")
At this point, the `db` object will be a connection to a MongoDB server for the specified database. Each DB instance uses a separate socket connection to the server.
If you're trying to connect to a replica set, see [Replica Sets in Ruby](http://www.mongodb.org/display/DOCS/Replica+Sets+in+Ruby).
### Authentication
2010-11-03 16:41:56 +00:00
MongoDB can be run in a secure mode where access to databases is controlled through name and password authentication. When run in this mode, any client application must provide a name and password before doing any operations. In the Ruby driver, you simply do the following with the connected mongo object:
auth = db.authenticate(my_user_name, my_password)
If the name and password are valid for the database, `auth` will be `true`. Otherwise, it will be `false`. You should look at the MongoDB log for further information if available.
## Using a Collection
2010-11-03 16:41:56 +00:00
You can get a collection to use using the `collection` method:
2010-11-03 16:41:56 +00:00
coll = db.collection("testCollection")
2010-11-03 16:41:56 +00:00
This is aliased to the \[\] method:
2010-11-03 16:41:56 +00:00
coll = db["testCollection"]
Once you have this collection object, you can now do create, read, update, and delete (CRUD) functions on persistent storage.
2010-11-03 16:41:56 +00:00
### Creating Documents with `insert`
2010-11-03 16:41:56 +00:00
Once you have the collection object, you can create or `insert` documents into the collection. For example, lets make a little document that in JSON would be represented as
2010-11-03 16:41:56 +00:00
{
"name" : "MongoDB",
"type" : "database",
"count" : 1,
"info" : {
x : 203,
y : 102
}
}
Notice that the above has an "inner" document embedded within it. To do this, we can use a Hash or the driver's OrderedHash (which preserves key order) to create the document (including the inner document), and then just simply insert it into the collection using the `insert` method.
2010-11-03 16:41:56 +00:00
doc = {"name" => "MongoDB", "type" => "database", "count" => 1, "info" => {"x" => 203, "y" => '102'}}
id = coll.insert(doc)
2010-11-03 16:41:56 +00:00
We have saved the `id` for future use below. Now the collection has been created and you can list it.
2010-11-03 16:41:56 +00:00
#### Getting a List Of Collections
2010-11-03 16:41:56 +00:00
Each database has zero or more collections. You can retrieve a list of them from the db (and print out any that are there):
2010-11-03 16:41:56 +00:00
db.collection_names
2010-11-03 16:41:56 +00:00
You should see
2010-11-03 16:41:56 +00:00
\["testCollection", "system.indexes"\]
2010-11-03 16:41:56 +00:00
#### Adding Multiple Documents
2010-11-03 16:41:56 +00:00
To demonstrate some more interesting queries, let's add multiple simple documents to the collection. These documents will have the following form:
2010-11-03 16:41:56 +00:00
{
"i" : value
}
2010-11-03 16:41:56 +00:00
Here's how to insert them:
2010-11-03 16:41:56 +00:00
100.times { |i| coll.insert("i" => i) }
2010-11-03 16:41:56 +00:00
Notice that we can insert documents of different "shapes" into the same collection. These records are in the same collection as the complex record we inserted above. This aspect is what we mean when we say that MongoDB is "schema-free".
2010-11-03 16:41:56 +00:00
### Reading Documents with `find_one` and `find`
2010-11-03 16:41:56 +00:00
#### Reading the First Document in a Collection using `find_one`
2010-11-03 16:41:56 +00:00
To retrieve the document that we inserted, we can do a simple `find_one` method to get the first document in the collection. This method returns a single document directly.
2010-11-03 16:41:56 +00:00
coll.find_one
2010-11-03 16:41:56 +00:00
and you should something like:
2010-11-03 16:41:56 +00:00
{"_id"=>BSON::ObjectId('4f7b1ea6e4d30b35c9000001'), "name"=>"MongoDB", "type"=>"database", "count"=>1, "info"=>{"x"=>203, "y"=>"102"}}
Note the `_id` element has been added automatically by MongoDB to your document.
2010-11-03 16:41:56 +00:00
#### Reading All of the Documents with a Cursor using `find`
2010-11-03 16:41:56 +00:00
To get all the documents from the collection, we use the `find` method. `find` returns a `Cursor` object, which allows us to iterate over the set of documents that matches our query. The Ruby driver's Cursor implemented Enumerable, which allows us to use `Enumerable#each`, `Enumerable#map}, etc. For instance:
2010-11-03 16:41:56 +00:00
coll.find.each { |row| puts row.inspect }
2010-11-03 16:41:56 +00:00
and that should print all 101 documents in the collection. You can take advantage of `Enumerable#to_a`.
2010-11-03 16:41:56 +00:00
puts coll.find.to_a
2010-11-03 16:41:56 +00:00
Important note - using `to_a` pulls all of the full result set into memory and is inefficient if you can process by each individual document. To process with more memory efficiency, use the `each` method with a code block for the cursor.
#### Specific Queries
2010-11-03 16:41:56 +00:00
We can create a _query_ hash to pass to the `find` method to get a subset of the documents in our collection. To check that our update worked, find the document by id:
2010-11-03 16:41:56 +00:00
coll.find("_id" => id).to_a
2010-11-03 16:41:56 +00:00
If we wanted to find the document for which the value of the "i" field is 71, we would do the following:
2010-11-03 16:41:56 +00:00
coll.find("i" => 71).to_a
2010-11-03 16:41:56 +00:00
and it should just print just one document:
{"_id"=>BSON::ObjectId('4f7b20b4e4d30b35c9000049'), "i"=>71}
#### Sorting Documents in a Collection
To sort documents, simply use the `sort` method. The method can either take a key or an array of [key, direction] pairs to sort by.
Direction defaults to ascending order but can be specified as Mongo::ASCENDING, :ascending, or :asc whereas descending order can be specified with Mongo::DESCENDING, :descending, or :desc.
# Sort in ascending order by :i
coll.find.sort(:i)
# Sort in descending order by :i
coll.find.sort([:i, :desc])
#### Counting Documents in a Collection
Now that we've inserted 101 documents (the 100 we did in the loop, plus the first one), we can check to see if we have them all using the `count` method.
coll.count
and it should print `101`.
2010-11-03 16:41:56 +00:00
#### Getting a Set of Documents With a Query
We can use the query to get a set of documents from our collection. For example, if we wanted to get all documents where "i" > 50, we could write:
puts coll.find("i" => {"$gt" => 50}).to_a
2010-11-03 16:41:56 +00:00
which should print the documents where i > 50. We could also get a range, say 20 < i <= 30:
puts coll.find("i" => {"$gt" => 20, "$lte" => 30}).to_a
2010-11-03 16:41:56 +00:00
#### Selecting a Subset of Fields for a Query
2010-11-03 16:41:56 +00:00
Use the `:fields` option to specify fields to return.
2010-11-03 16:41:56 +00:00
puts coll.find("_id" => id, :fields => ["name", "type"]).to_a
2010-11-03 16:41:56 +00:00
#### Querying with Regular Expressions
Regular expressions can be used to query MongoDB. To find all names that begin with 'a':
puts coll.find({"name" => /^M/}).to_a
2010-11-03 16:41:56 +00:00
You can also construct a regular expression dynamically. To match a given search string:
params = {'search' => 'DB'}
2010-11-03 16:41:56 +00:00
search_string = params['search']
# Constructor syntax
puts coll.find({"name" => Regexp.new(search_string)}).to_a
2010-11-03 16:41:56 +00:00
# Literal syntax
puts coll.find({"name" => /#{search_string}/}).to_a
2010-11-03 16:41:56 +00:00
Although MongoDB isn't vulnerable to anything like SQL-injection, it may be worth checking the search string for anything malicious.
### Updating Documents with `update`
We can update the previous document using the `update` method. There are a couple ways to update a document. We can rewrite it:
doc["name"] = "MongoDB Ruby"
coll.update({"_id" => id}, doc)
Or we can use an atomic operator to change a single value:
coll.update({"_id" => id}, {"$set" => {"name" => "MongoDB Ruby"}})
Verify the update.
puts coll.find("_id" => id).to_a
Read [more about updating documents|Updating].
### Deleting Documents with `remove`
Use the `remove` method to delete documents.
coll.count
coll.remove("i" => 71)
coll.count
puts coll.find("i" => 71).to_a
The above shows that the count has been reduced and that the document can no longer be found.
Without arguments, the `remove` method deletes all documents.
coll.remove
coll.count
Please program carefully.
2010-11-03 16:41:56 +00:00
## Indexing
### Creating An Index
2010-11-03 16:41:56 +00:00
MongoDB supports indexes, and they are very easy to add on a collection. To create an index, you specify an index name and an array of field names to be indexed, or a single field name. The following creates an ascending index on the "i" field:
# create_index assumes ascending order; see method docs
# for details
coll.create_index("i")
To specify complex indexes or a descending index you need to use a slightly more complex syntax - the index specifier must be an Array of [field name, direction] pairs. Directions should be specified as Mongo::ASCENDING or Mongo::DESCENDING:
# Explicit "ascending"
coll.create_index([["i", Mongo::ASCENDING]])
Use the `explain` method on the cursor to show how MongoDB will run the query.
coll.find("_id" => id).explain
coll.find("i" => 71).explain
coll.find("type" => "database").explain
The above shows that the query by `_id` and `i` will use faster indexed BtreeCursor, while the query by `type` will use a slower BasicCursor.
### Getting a List of Indexes on a Collection
You can get a list of the indexes on a collection.
coll.index_information
### Creating and Querying on a Geospatial Index
2010-11-03 16:41:56 +00:00
First, create the index on a field containing long-lat values:
people.create_index([["loc", Mongo::GEO2D]])
Then get a list of the twenty locations nearest to the point 50, 50:
people.find({"loc" => {"$near" => [50, 50]}}, {:limit => 20}).each do |p|
puts p.inspect
2010-11-03 16:41:56 +00:00
end
## Dropping
### Drop an Index
To drop a secondary index, use the `drop_index` method on the collection.
coll.drop_index("i_1")
coll.index_information
The dropped index is no longer listed.
### Drop All Indexes
To drop all secondary indexes, use the `drop_indexes` method on the collection.
coll.drop_indexes
coll.index_information
Only the primary index "_id_" is listed.
### Drop a Collection
To drop a collection, use the `drop` method on the collection.
coll.drop
db.collection_names
The dropped collection is no longer listed. The `drop_collection` method can be used on the database as an alternative.
db.drop_collection("testCollection")
### Drop a Database
To drop a database, use the `drop_database` method on the connection.
connection.drop_database("mydb")
connection.database_names
The dropped database is no longer listed.
2010-11-03 16:41:56 +00:00
## Database Administration
A database can have one of three profiling levels: off (:off), slow queries only (:slow_only), or all (:all). To see the database level:
puts db.profiling_level # => off (the symbol :off printed as a string)
db.profiling_level = :slow_only
Validating a collection will return an interesting hash if all is well or raise an exception if there is a problem.
p db.validate_collection('coll_name')
## See Also
* [MongoDB Koans](http://github.com/chicagoruby/MongoDB_Koans) A path to MongoDB enlightenment via the Ruby driver.
* [MongoDB Manual](http://www.mongodb.org/display/DOCS/Developer+Zone)