NoSQL databases are often a perfect fit for managing your application's data, as it allows you to work with objects instead of tables. In addition to the benefit of working with objects, using NoSQL on mobile reduces complexity by removing the need for schema management, data migration, and other common maintenance issues associated with maintaining a relational data model.
MongloDb is a pure JavaScript implementation of the popular MongoDb NoSQL solution. The MongloDb
module allows you to query and persist objects using the familiar MongoDb syntax in your Titanium app. This recipe demonstrates how to leverage the MongloDb
module within your new or existing Titanium project.
Installing MongloDb for Titanium is a straightforward process. You can either copy the Monglo
folder from the recipe's source code, or create the bundle yourself.
Creating the bundle is a three-step process:
Monglo
in the Resources
folder of your project.monglodb.js
file from monglodb.com into the Monglo
folder created in the previous step.Monglo
folder the latest index.js
file from the Titanium Store project available on monglodb.com.Whether you copied the bundle from the recipe source or created your own, the Monglo
folder in your Titanium project should resemble the highlighted part of the following screenshot:
Once you have installed MongloDb, you will need to use require
to import the module into your code:
//Create our application namespace var my = { monglo : require('./Monglo/monglodb').Monglo };
After the module has been imported, a new instance of the database needs to be initialized. The following code demonstrates how to create a new instance of Monglo
with the name myDb
:
my.db = my.monglo('myDb'),
MongloDb has the ability to support a wide range of storage providers. This recipe implements the Titanium Store provider to persist data. Associating a storage provider with MongloDb is a two-step process. First, we require
the Titanium storage provider, as shown here:
var tistore = require('./Monglo/index'),
After the storage provider has been created, the provider is passed into the use
method, as demonstrated in the following statement. Once the use
method is called, MongloDb will perform all persistence operations using the Titanium Storage provider.
my.db.use('store', tistore);
Once the storage provider has been associated, durable collections can be created using the collection
method. The following line demonstrates how to create a document collection named foo
:
my.db.someCollection = my.db.collection('foo'),
When initializing a named collection, any documents previously persisted by this collection will automatically be reloaded. In the previous example, any documents previously saved in the foo
document collection will be reloaded when the collection is initialized.
MongloDb provides events for monitoring a majority of the actions performed. The following snippet demonstrates how to add an event for each supported listener:
my.db.someCollection.on('insert', function(){ Ti.API.info("Document Inserted") ; }); my.db.someCollection.on('update', function(){ Ti.API.info("Document Updated"); }); my.db.someCollection.on('remove', function(){ Ti.API.info("Document Removed"); }); my.db.someCollection.on('find', function(){ Ti.API.info("Find Used"); }); my.db.someCollection.on('createCollection', function(){ Ti.API.info("Collection Created"); }); my.db.someCollection.on('removeCollection', function(){ Ti.API.info("Collection Removed"); });
The next step is to insert three new documents. The next snippet demonstrates how to insert a new document into someCollection
. The insert
method has two parameters. The first parameter is a document object to be stored. The second parameter is a callback that lists the errors and provides document information. This is shown in the following highlighted snippet:
my.db.someCollection.insert({text: "record 1", batchId:'sample_test'}, function ( error, doc )
The error
and doc
objects are returned as part of the callback
function. The error
object contains any issues encountered during the insert action, and the doc
object contains a copy of the Monglo
document created.
Ti.API.info('Error: ' + JSON.stringify(error)); Ti.API.info('doc: ' + JSON.stringify(doc)); }); //Create second record my.db.someCollection.insert({text: "record 2", batchId:'sample_test'}, function ( error, doc ){ }); //Create third record my.db.someCollection.insert({text: "record 3", batchId:'sample_test'}, function ( error, doc ){ });
With three records now created, we can use the find
function to search for all the documents that have the batch ID sample_test
.
my.db.someCollection.find({batchId:'sample_test'}, function ( error, cursor ){
The find
method returns both an error
and cursor
object. The cursor's forEach
iterator provides a convenient way to inspect each document that is returned. The following snippet demonstrates how to print each document within the cursor
to the Titanium Studio console as a JSON string:
cursor.forEach(function(doc){ Ti.API.info('doc: ' + JSON.stringify(doc)); }); });
Similar to a traditional database, MongloDb provides the ability to change a document using the update
method. The update
method has three parameters. The first parameter is used to find the object you wish to update. The next example shows updating any object with the text property equal to record 1
. The second parameter is the update statement. The example updates each matching object's text property to updated record 1
. The final parameter is a callback which returns an error
and doc
object.
my.db.someCollection.update({text: "record 1"}, {$set: {text: 'updated record 1'}}, function ( error, doc ){ Ti.API.info('Error: ' + JSON.stringify(error)); Ti.API.info('doc: ' + JSON.stringify(doc)); });
This demonstrated how to update the text property on the first document inserted in this recipe.
The findOne
method provides the means to query your collection for a specific document. The following snippet demonstrates how to query someCollection
for the document we just updated. The resulting single-matching document is then printed to the Titanium Studio console as a JSON string.
my.db.someCollection.findOne({text: 'updated record 1'}, function ( error, doc ){ Ti.API.info('Error: ' + JSON.stringify(error)); Ti.API.info('doc: ' + JSON.stringify(doc)); });
The final section of this recipe demonstrates how to remove documents from a collection. The following snippet demonstrates how to remove all documents that have the batch ID sample_test
.
my.db.someCollection.remove({batchId:'sample_test'}, function (error) { Ti.API.info('Error: ' + JSON.stringify(error)); });
All the NoSQL examples shown in this recipe use the MongloDb open source project.Please see the following to learn more about this project:
3.145.46.109