Posted on by & filed under Content - Highlights and Reviews, Programming & Development, Web Development.

MongoDB is a NoSQL document-oriented database. In previous articles we introduced MongoDB and also covered in detail how to use MongoDB’s unique data model to store structured data. In this article we will explore the programming environment that MongoDB provides as well as cover the programmatic interaction with the database using Mongo Shell.

Client and Libraries

MongoDB is an open source software and its underlying architecture and protocols are well documented. This has given rise to a plethora of clients and libraries for various programming languages available for use with MongoDB. A very comprehensive listing is available at http://www.mongodb.org/display/DOCS/Drivers. Scroll down the page to the section for your respective programming language.

There are some interesting tools and frameworks available that are built around MongoDB. One such tool is the Opa programming language. Opa is a new kind of language designed specifically for Web applications that allows writing code in one language which is then compiled into code for the server-side and client-side. Opa has built-in support for MongoDB and allows developers to seamlessly use it in their applications. Similarly, Lithium is a PHP framework that is based upon the Model-View-Controller (MVC) pattern. It is inspired from the CakePHP framework. Lithium framework has a custom ORM which has built-in support for MongoDB. If you are looking to develop your next web application with PHP (which will use MongoDB) then give the Lithium framework a try.

The Mongo Shell

The Mongo shell is a command line tool that comes with MongoDB. If everything is installed properly it can be accessed by typing in the command line:

The Mongo shell is a full fledged JavaScript interpreter that allows programmatic access to MongoDB. When invoked without any parameters, it connects to the localhost and opens the “test” database. To connect to a database running on another server use the –host and –port flags:

You can specify the database to be used by supplying its name as a parameter:

Or once the mongo shell is running you can run the use command to switch to another database:

Saving and Retrieving Documents

As explained before, the Mongo shell is a JavaScript interpreter, so it can execute any valid JavaScript command. Access to the database is also exposed through an object on which operations can be performed. This is the “db” object. For example, try typing the following:

It will output the current database name (testdb in our case). We can similarly perform other database operations on the db object.

To insert a document into a collection “books” we can use:

Here the object “books” refers to the collections names. This object (or collection) does not have to be created prior to use. Mongo shell uses what’s called lazy loading. The collection object is automatically created on first use. The “save” method is used to save or insert data into a collection. It takes one parameter, which is the document object to be saved. In the example above we have used JavaScript’s object literal notation to create a document object and pass it to the save method in one line. We could have used something like the following as well:

To fetch or retrieve data just stored, we could use one of the “find” methods (find, findOne and findAndModify):

This will return a list of documents which are stored in the books collection. The find method can be provided an optional parameter: query. This parameter can be used to filter the documents based on a criteria. For example,

The query parameter is actually a document itself which specifies the values to search for each field. In this case, we have searched for the title which contains ‘MongoDB’ using regular expressions. This query will correctly return the document we just stored. Another article treats querying in MongoDB in detail.

To update a document we can combine the search and save operations using the “update” method. This method takes two parameters: the query and the updated document. For example:

Executing the find method will now show the updated title.

Conclusion

We explored the basic programming environment available with MongoDB and saw examples of common operations such as insert, update and retrieve.

Safari Books Online has the content you need

Below are some MongoDB books to help you develop applications, or you can check out all of the MongoDB books and training videos available from Safari Books Online. You can browse the content in preview mode or you can gain access to more information with a free trial or subscription to Safari Books Online.

MongoDB in Action is a comprehensive guide to MongoDB for application developers. The book begins by explaining what makes MongoDB unique and describing its ideal use cases. A series of tutorials designed for MongoDB mastery then leads into detailed examples for leveraging MongoDB in e-commerce, social networking, analytics, and other common applications.
MongoDB and Python is a cookbook-style text to help Python programmers work with MongoDB. It is full of useful, practical recipes for solving real-world problems ranging from how to do fast geo queries for location-based apps to efficiently indexing your user documents for social-graph lookups to how best to integrate MongoDB with the Pyramid Web framework.
Learn how to create large MongoDB clusters! Scaling MongoDB shows you how to use MongoDB efficiently for very large databases. It Covers sharding, cluster setup, and administration.

About the authors

Salman Ul Haq is a techpreneur, co-founder and CEO of TunaCode, Inc., a startup that delivers GPU-accelerated computing solutions to time-critical application domains. He holds a degree is Computer Systems Engineering. His current focus is on delivering the right solution for cloud security. He can be reached at salman@tunacode.com.
Shaneeb Kamran is a Computer Engineer from one of the leading universities of Pakistan. His programming journey started at the age of 12 and ever since he has dabbled himself in every new and shiny software technology he could get his hands on. He is currently involved in a startup that is working on cloud computing products.

Tags: CakePHP, Javascript, Lithium, MongoDB, Opa,

Comments are closed.