One of the goals of the MongoDB OML is to allow you to write applications using a Facets-based, persistent, object oriented data model. At the core of this model are MongoDBObject-based classes written using persistent DBxxx facets.
The attraction of such a model is that it allows you to focus your attention on desiging and implementing your application data model and logic without having to deal with the technical details involved with accessing the underlying database storage layer.
That’s the dream. But the reality is that there is an underlying database storage layer, and sometimes you have to get your hands dirty grubbing around with it. That’s where the MongoDB class comes in.
A MongoDB instance is an object representing your application’s connection to a particular MongoDB server. In many cases, such as the example application we looked at earlier, there are no direct references to a MongoDB object to be found anywhere in the code. But you can rest assured that such an object exists, managing the data flow between your application and the MongoDB server under the covers.
If completely transparent access to a MongoDB object were always the case, then we could end this discussion now. But there are many cases where you may need to use a MongoDB object directly to manage some aspect of your application’s interaction with its associated MongoDB server. So we are going to spend the rest of this section presenting the complete, public API exposed by the MongoDB class.
Let’s get started with the public facets exposed by a MongoDB object...
With one exception, all of the public facets exposed by a MongoDB object are used for configuring access to a particular MongoDB server. Since these are the facets you are most likely to use, we’ll start with them:
The only other publically accessible facet provided by a MongoDB object is:
Saves all of the the MongoDBObject instances provided as arguments to the associated MongoDB database. If no arguments are specified, any currently dirty objects waiting to be saved to the database are saved, and the list of dirty objects is reset to empty.
A dirty object is any MongoDBObject instance loaded from the database which has subsequently been modified by the application in some way.
The number of objects successully saved to the database is returned as the result.
Closes the connection to the associated MongoDB server.
It returns self as the result.
Shuts down the MongoDB database process only if it was started by the application using the auto_start option.
It returns self as the result.
Resets the contents of the database to an empty state, but retains all accumulated schema information. All data contained in the database will be lost. This method is mainly provided for use during application developement and testing to help ensure the database is in a known state.
It returns self as the result.
This is a convenience method that allows an end user to interactively configure MongoDB database connection information. It returns self as the result after any user configuration has been performed.
The configuration information is looked up in and saved to the Facets database using the application name specified by application. If force is True, the user is always prompted for the configuration information, using the existing configuration data as the default if it already exists. Otherwise, the user is only prompted for the configuration information if it is not already in the Facets database.
For simple application’s that require some straightforward user configuration, you can use this method at application start-up time to help ensure that a properly set-up MongoDB object is available. For more complex applications, you may want to provide your own configuration scheme.
The following shows an example of the method being used to configure a ‘document_index’ application in conjunction with the mongodb function:
mongodb().configure( 'document_index' )
This screen shot shows both pages of the user interface displayed when the method is invoked:
To help make management of the underlying MongoDB server connection as transparent as possible, MongoDB OML provides a mongodb function for getting and setting the default MongoDB object used by an application:
Gets or sets the implicit MongoDB object used by the application. If db is a MongoDB object, it sets the MongoDB database to use to db.
It always returns the most recently set MongoDB object. If no MongoDB object has been previously set, one is created and returned.
As a safety net, the MongoDB OML installs a handler invoked at application termination that automatically performs save, close and shutdown method calls on the MongoDB object returned by mongodb(). This helps ensure that any modified objects get flushed to the MongoDB database when the application exits.
However, because the Python runtime does not guarantee that exit handlers are always called, it is a very good idea to provide application level code that ensures the MongoDB database state has been properly saved prior to terminating an application.