Skip to Navigation

MongoDB: A Simple User Directory

In my last post, I glossed over MongoDB from a very high level. While it's useful to know how MongoDB works, I didn't cover exactly how to use it.

There are a growing number of NoSQL systems, and one of MongoDB's greatest strengths is its ease-of-use. It takes a couple of minutes to install the server, and commands are given using the familiar JSON syntax.

Setting up MongoDB

I'm running Windows, but installing on a Mac or *nix system shouldn't be much different. After downloading the appropriate package, copy the mongdb folder into c:/data/db/. Then, using your shell (cmd prompt), browse to c:/data/db/mongodb/bin and execute the server executable, mongod.exe:

Voila! The MongoDB server is now up and running. Let's fire up a separate window, this time for an instance of the MongoDB client. The client is mongo.exe, also in the bin directory.

Setting up a User Directory

We want to create a simple user directory, and want to store the following information about each user:

name
age
languages

Unlike with traditional SQL systems, we don't have to create a database schema. You simply create a JSON object containing the data you want, and MongoDB saves it as a document. By default, MongoDB will use the test database. Let's change that to a new database called users:

use users;

I know, it was pretty intense. Once you've wiped the sweat from your brow, let's add some data to our new database:

 var data = [
    {name: "John Doe", age: 28, languages: ["English", "Arabic"]},
    {name: "Mike Smith", age: 35, languages: ["English"]},
    {name: "Sally Mae", age: 41, languages: ["Italian", "French", "Arabic"]}
];

 To add these people to our database, we run the following command:

db.users.save(data);

Getting all users

 To find all items in the collection, you'd run db.users.find().

This returns our users. If there are more than 10 users, you'll need to enter the "it" command to iterate through the rest of the results. Fortunately, the MongoDB PHP extension makes iterating a lot less tedius.

Getting specific users

You're able to enter specific parameters into the find() function to retrieve filtered results.

To find "John Doe":

db.users.find({name: "John Doe"});

To find anyone 41 years old:

db.users.find({age: 41});

To find anyone who speaks Arabic:

db.users.find({languages: "Arabic"});

MongoDB also supports comparison operators (<, >, <=, >=, etc), but the format takes a bit to get used to. For example, let's find users between 30 and 40 years old:

db.users.find({age: {'$gte': 30}, {'$lte': 40}});

NOTE: "Less than" is '$lt', whereas "Less than or equal" is '$lte'.

Snafus

When using MongoDB, you'll run into difficulties that were non-issues in MySQL. One example is using "OR" when building queries. In MySQL, it's quick and painless to use OR when finding records. Let's find users <= 30 or >= 40 years old:

MySQL

SELECT * FROM users WHERE age <= 30 OR age >= 40;

MongoDB

The "OR" operator was just added to a nightly build of MongoDB, but is unavailable in the current stable version. It's still possible, but it requires a custom '$where' function:

db.users.find({'$where': function() { return this.age <= 30 || this.age >= 40; }});

Conclusion

MongoDB seems to be a pretty powerful tool for document storage. It's fairly robust, extremely fast (from what I can tell), and easy to use.

I cannot stress enough that MongoDB (or any NoSQL solution) is not a substitute for all things SQL. If your application has transactional or relational requirements, you're better off sticking with SQL.