At the Forge - MongoDB
The find_one method, as you have seen, returns a single element from a collection. A similar find method returns all of the elements using the Enumerable module, allowing you to iterate over all of the documents in a collection using each. For example, if you add another document:
irb(main):026:0> c.insert({'name' => 'Reuven',
'email_address' => 'reuven@lerner.co.il'})
=> 4b6ff0693c1c7d6ecd000001
you can retrieve the IDs as follows:
irb(main):030:0> c.find.each {|i| puts i['_id']}
4b6fe8983c1c7d6a6a000001
4b6ff0693c1c7d6ecd000001
Notice how you can pull out the _id column by treating the document as a hash. Indeed, if you ask Ruby to show the class of the object, rather than its ID, this suspicion is confirmed:
irb(main):031:0> c.find.each {|i| puts i.class}
OrderedHash
OrderedHash
But, perhaps you're interested only in some of the documents. By invoking find with a hash, it will return only those documents that match the contents of your hash. For example:
irb(main):040:0> c.find({'name' => 'Reuven'}).count
=> 1
If nothing matches the hash that you passed, you will get an empty result set:
irb(main):041:0> c.find({'name' => 'Reuvennn'}).count
=> 0
You also can search for regular expressions:
irb(main):042:0> c.find({'name' => /eu/}).count
=> 1
irb(main):043:0> c.find({'name' => /ez/}).count
=> 0
By passing a hash as the value for a key, you also can modify the query, passing parameters that define MongoDB's query syntax. These query operators all begin with the dollar sign ($) and are passed as the key to a sub-hash. For example, you can retrieve all of the documents whose “name” is one of the values in a specified array, as follows:
irb(main):049:0> c.find({'name' =>
{'$in' => ['Reuven', 'Atara', 'Shikma',
↪'Amotz'] } }
).count
=> 1
You also can sort the results by invoking the sort method on the result set, using a similar syntax:
irb(main):049:0> c.find({'name' =>
{'$in' => ['Reuven', 'Atara', 'Shikma',
↪'Amotz'] } }
).sort({"name" => 1})
Just as you can sort a result set, you also can perform other actions on it that are analogous to several relational counterparts, such as grouping and limiting the number of results. If you are used to a functional style of programming, in which you chain a number of methods to one another, this style easily will lend itself to working with MongoDB.
MongoDB is causing many ripples in the open-source and database worlds because of its high performance and easy learning curve. This month, I covered the basics of installing and working with MongoDB. Next month, I'll look at some more-advanced topics, such as indexing (which makes queries execute much faster), embedding objects in one another and referencing objects across collections.
Resources
The main site for MongoDb, including source code and documentation, is at mongodb.org.
The Ruby driver for MongoDB is hosted at GitHub: github.com/mongodb/mongo-ruby-driver. The Python drivers are at github.com/mongodb/mongo-python-driver.
For an excellent introduction to MongoDB, including some corporate background on 10gen and how it can be used in your applications, listen to episode 105 of the “FLOSS Weekly” podcast. I found the podcast to be both entertaining and informative.
Another good introduction is from John Nunemaker, a well-known blogger in the Ruby world: railstips.org/blog/archives/2009/06/03/what-if-a-key-value-store-mated-with-a-relational-database-system.
A blog posting having to do with MongDB's speed, relative to that of CouchDB and MySQL is at www.idiotsabound.com/did-i-mention-mongodb-is-fast-way-to-go-mongo.
Finally, I continue to think that there are parallels between dynamic languages and document-oriented databases. Steve Yegge, an engineer at Google, has written about the growing popularity of dynamic languages, and I strongly recommend his presentation for an interesting perspective on the issue: steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html.
Reuven M. Lerner is a longtime Web developer, trainer, and consultant. He is a PhD candidate in Learning Sciences at Northwestern University. Reuven lives with his wife and three children in Modi'in, Israel.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- Web & UI Developer (JavaScript & j Query)
- UX Designer
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




1 hour 45 min ago
7 hours 31 min ago
7 hours 49 min ago
9 hours 42 min ago
11 hours 35 min ago
18 hours 29 min ago
18 hours 46 min ago
20 hours 37 min ago
1 day 2 hours ago
1 day 7 hours ago