|
Key
This line was removed.
This word was removed. This word was added.
This line was added.
|
Changes (38)
View page history| This tutorial gives many common examples of with MongoDB using MongoDB with the Ruby driver. If you're looking for information on data modeling, see [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]. |
| 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/]. |
| Interested in GridFS? Checkout [GridFS in Ruby]. |
| 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/]. |
| {toc} h2. Installation |
| The mongo-ruby-driver gem is served through Gemcutter. If you haven't installed gemcutter: |
| The mongo-ruby-driver gem is served through Rubygems.org. To install, make sure you have the latest version of rubygems. |
| {code} gem install gemcutter update --system |
| gem tumble |
| {code}Next, install the mongo and mongo_ext rubygems: rubygem: |
| {code} gem install mongo |
| gem install mongo_ext {code}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. |
| {code} The required {{bson}} gem will be installed automatically. |
| For optimum performance, install the bson_ext gem: {code} gem install bson_ext {code} 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. |
| The full API documentation can be viewed [here|http://api.mongodb.org/ruby/index.html]. |
... |
| {code}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 pair, set, see [Replica Pairs Sets in Ruby]. |
h4. Listing All Databases |
| {code}m {code}connection = Mongo::Connection.new # (optional host/port args) |
| m.database_names.each connection.database_names.each { |name| puts name } |
| m.database_info.each connection.database_info.each { |info| puts info.inspect} |
| {code} h4. Dropping a Database |
| {code}m.drop_database('database_name') {code}connection.drop_database('database_name') |
| {code} h4. Authentication (Optional) |
... |
| h4. Finding the First Document In a Collection using {{find_one()}} |
| To show that the document we inserted in the previous step is there, we can do a simple {{find_one()}} operation to get the first document in the collection. This method returns a single document (rather than the {{Cursor}} that the {{find()}} operation returns), and it's useful for things where there only is one document, or you are only interested in the first. You don't have to deal with the cursor. returns). |
{code}my_doc = coll.find_one() |
... |
| {code}and you should see: |
| {code}{"_id"=>#<Mongo::ObjectID:0x118576c {code}{"_id"=>#<BSON::ObjectID:0x118576c ...>, "name"=>"MongoDB", "info"=>{"x"=>203, "y"=>102}, "type"=>"database", "count"=>1} |
| {code}Note the {{\_id}} element has been added automatically by MongoDB to your document. Remember, MongoDB reserves element names that start with _ for internal use. |
h4. Adding Multiple Documents |
| In order to do more interesting things with queries, let's add multiple simple documents to the collection. These documents will just be |
| To demonstrate some more interesting queries, let's add multiple simple documents to the collection. These documents will have the following form: |
| {code}{ "i" : value } |
| {code}and we can do this fairly easily: |
| {code} |
| Here's how to insert them: |
| {code}100.times { |i| coll.insert("i" => i) } {code}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". |
... |
| h4. Using a Cursor to get all of the Documents |
| In order to get all the documents in the collection, we will use the {{find()}} method. The {{find()}} method returns a {{Cursor}} object which allows us to iterate over the set of documents that matched our query. The Ruby driver's Cursor is enumerable. So to query all of the documents and print them out: |
| 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: |
{code}coll.find().each { |row| puts row.inspect } |
... |
| {code}and it should just print just one document: |
| {code}{"_id"=>#<Mongo::ObjectID:0x117de90 {code}{"_id"=>#<BSON::ObjectID:0x117de90 ...>, "i"=>71} |
| {code} h4. Getting a Set of Documents With a Query |
... |
| {code} |
| Although MongoDB isn't vulnerable to anything like SQL-injection, it may be worth checking the search string for anything malicious. |
| h4. Creating An Index |
... |
{code}# explicit "ascending" |
| coll.create_index([["i", Mongo::ASCENDING]]) |
| {code} |
h4. Creating and querying on a geospatial index First, create the index on a field containing long-lat values: {code} people.create_index([["loc", Mongo::GEO2D]]) {code} Then get a list of the twenty locations nearest to the point 50, 50: {code} people.find({"loc" => {"$near" => [50, 50]}}, {:limit => 20}).each do |p| puts p.inspect end {code} |
| h4. Getting a List of Indexes on a Collection |
... |
| h4. Database Administration |
| A DB object class has an admin method that returns an object that can perform administrative functions. {code}admin = db.admin |
| {code}A A database can have one of three profiling levels: off (:off), slow queries only (:slow_only), or all (:all). To see the database level: |
| {code}puts admin.profiling_level db.profiling_level # => off (the symbol :off printed as a string) |
| admin.profiling_level db.profiling_level = :slow_only |
| {code}Validating a collection will return an interesting hash if all is well or raise an exception if there is a problem. |
| {code}p admin.validate_collection('coll_name') db.validate_collection('coll_name') |
| {code} h2. See Also |
| * [Ruby Driver RDoc Official Docs |http://api.mongodb.org/ruby/index.html] |
| * [MongoDB Koans|http://github.com/chicagoruby/MongoDB_Koans] A path to MongoDB enlightenment via the Ruby driver. |
| * [MongoDB Manual|Developer Zone] |
