NoSQL: HuMONGOus Benefits (Part 2)
At Fellowship Technologies we’ve utilized NoSQL as a persistent caching mechanism. We used MongoDB as our NoSQL data store. MongoDB is a document-based schema-less data store. MongoDB was an ideal solution because we had a dataset where we needed to cache a dataset with a varying set of fields.
Installing Mongo
To install MongoDB go to the MongoDB site and choose the right version for your platform. If you have an OS with 64-bit capabilities then you should choose a 64-bit version of MongoDB because the 32-bit version has a 4GB memory limitation. Since MongoDB relies on memory mapped files the 4GB limit doesn’t necessarily pertain to the size of your database. Extract the MongoDB files from the archive. Go into the bin folder and run mongod to run the mongodb server. Specify the —dbpath flag to specify the directory where the data files will be created and stored. I.e.
mongod --dbpath=../db
Once the server is running you can then connect to it with the mongo executable. When you run the mongo executable without specifying a host or database it will automatically connect to your local machine and the database: test.
Using Mongo
When you’ve connected to MongoDB you are ready to start querying the data store. If you want to switch databases, type “use database”. While using the shell you can type the following commands to get help:
db.help();
db.CollectionName.help();
Let’s put some data in our MongoDB instance. To do this you need to use the db.
db.ExampleCollection.insert({FirstName: "Jesse", LastName: "Dearing", Age: 26});
This will insert a record with the specified fields plus a field called _id that is created by default with the ObjectId object. You can use _id to be whatever you want though, as long as the value is unique. As MongoDB is a schema-less data store we can insert another record with completely different fields.
db.ExampleCollection.insert({Question: "How much more cool could this be?", Answer: "None. None more cool."});
Now we can look at what has been inserted into the collection using the find command. With no arguments find will return everything.
db.ExampleCollection.find();
If you wanted to search on different fields, you would specify them as a hash just like the insert record. So to search on my last name is:
db.ExampleColletion.find({LastName: "Dearing"});
In MongoDB you have to chain methods to construct queries. For example if you want to sort by last name ascending:
db.ExampleCollection.find().sort({LastName: 1});
If you wanted to sort descending you would pass in a -1 to the sort method. You can see more about what queries you can write in the MongoDB Querying documentation.
Lessons Learned
During our use of MongoDB we learned a few lessons about the tech.
First and foremost, use the 64-bit version. The 64-bit version does not have the limits that the 32-bit version does (due to the nature of 32-bit applications).
Second, writes are lightning fast. Because writes are so fast, there must be a place where the sacrifice is made. The place where the sacrifice is made is atomicity during removes. Removes run concurrently with other operations, but if you’re not removing data based on a unique criterion such as the _id field you might run into problems with your data not deleting when you think it should be.
Finally, just because MongoDB is a NoSQL data store, doesn’t mean that you can disregard indexes. If you don’t index fields that you’re searching on you will end up scanning the whole table.
Summary
MongoDB is a great data store and I have found it to be relatively easy to use and very useful in many applications. That being said using a schema-based data store with ACID compliance is sometimes necessary (storing anything involving money). NoSQL should be thought of as “Not only SQL”. As such MongoDB is an excellent tool to have in your toolbox, but don’t think of it as a replacement for other tools when they make sense.
Jesse Dearing is a Developer for Fellowship Technologies. Currently he is focused on DevOps which is where the needs of the infrastructure meet the development of the software. He likes learning about new technologies and languages (especially ones that are used on the web) as well as learning about new trends and practices in software development. He is passionate about software development and helping other developers.
Posted In: News
No one has commented yet. Be the first!
- Building a custom login for your church website using the API
November 29, 2011 - Roll Foward!
August 9, 2011 - The Agile Triangle
July 27, 2011 - Conversation Paralysis
July 7, 2011 - Picture this, image updates & creates through the REST API
May 10, 2011
2011
2010
- January (1)
- February (3)
- March (2)
- April (3)
- May (5)
- June (6)
- July (3)
- August (7)
- September (4)
- October (1)
