Advanced MongoDB and the Lithium Framework
So I've been neglecting my blog. I've just been extremely swamped. I want to redesign my entire personal site and blog here. I'm going to rebrand a little bit. I have to get some new business cards for SXSW and more... Anyway, before I do all that I figured I'd post something useful for everyone first.
I've been using a model class, though it could be more of a utility sitting in the "extensions" directory, that helps you work with MongoDB in more advanced ways. This includes some command line calls like mongoimport. Expect to see some more updates to this file as I have the import, but not export. So I'll have that added soon along with some other goodies.
So what can you do with this class? Well, let's start with the import command. You can now easily bundle a JSON file in your project's repo and have a command to set all that initial dependency data. I've also used it to import from a file that I had some other code build first because there were a lot of inserts and the import was faster. You could also say....use it along with a command class method to import from the Twitter streaming API. Pretty exciting right? If you saw the MongoSV keynote then you saw how the JSON feed that Twitter's API provides could very easily be used in the command line with mongoimport. Well, this method here makes it very easy for your application to do the same.
The other methods in this class allow you to work with stored JavaScript and execute arbitrary commands. A feature some of you may not know about is the ability to execute JavaScript in MongoDB. It's slower, and single threaded (does not -yet- use the V8 engine), but it's very convenient. In fact, it's how I've managed to pull off a few things like search engines and various machine learning algorithms right from within MongoDB. The stored JavaScript feature basically allows you to write your own functions to call in any future command. This class allows you to add and remove these stored functions in the database.
This class also lets you easily use MongoCode for map reduce, group, and other commands from external JavaScript files or just a string that you pass. External files are helpful because you don't need to write JavaScript inside PHP...It's cleaner and your code editor will pick up on syntax coloring properly. It's also just better for organization and maintanence.
I didn't set up a repository for this one, it's just a single file, for now. You can grab the code here. Be sure to watch the namespace up top and adjust for your application if needed.


[Back To Blog Index]