{ DreamFactory: 'Blog' }

DreamFactory 2.0 Bundled With MongoDB

Posted by Ben Busse

Fri, Nov 6, 2015

All of the new Bitnami packages for DreamFactory 2.0 have MongoDB built into the installation. Just run the installer, open up the admin console, and MongoDB is right there as one of the available services. This blog post discusses why we bundled MongoDB in the latest version of DreamFactory and how you can to use this great NoSQL database.

The Big Three

DreamFactory 2.0 has the world’s best REST API for old-fashioned SQL databases. Our enterprise customers often need to mobilize legacy SQL, and we want to make that super easy for them. But SQL can be a bit painful. First, you have to set up the schema. Our admin console has a tab to edit SQL schema, and of course there are many tools for this, but the tables and fields have to be designed carefully. And then there are other issues including date formats, required fields, stored procedures, primary keys, child and parent objects, etc.

And we also have great support for file storage systems. You can store whatever you want in there. We have a REST API for the local storage on your server, and also support a host of cloud storage options from S3 to OpenStack. But while you can save whatever you want in the file store, there isn’t any structure to the documents, and no easy way to search for things.

This is why we are proud to bundle MongoDB with DreamFactory 2.0. Now minimum hassle, you can leverage the immense and very flexible power of MongoDB for your applications. Just run the Bitnami installer and check out the services tab, pictured above. We also include a default SQL database, along with access to the local file storage system. This way, developers have “out of the box” access to SQL, NoSQL, or file storage as needed for their application design. But I think that when you take a look at the power and ease of use of MongoDB, you will see why it occupies the “sweet spot” between file storage and SQL.

Create Your First Table

You really only need to do one thing to start using MongoDB, and that is to create at least one table. (In MongoDB language, this is called a “collection,” but since DreamFactory has to deal with lots of different data sources, we have a more generic name.) There are two main ways to create a table. First, go to the API Docs tab in the admin console, and scroll down to the section:

/mongodb/_schema

Next, type the JSON below into the window and click the “Try it out” button.

Alternatively, you can just POST the JSON from any client. That way you could create tables programmatically, or even automate the process. The POST would look something like this. You will want to insert your own table name:

http://myserver.com:80/api/v2/mongodb/_schema

{ "resource": [{
     "name": "mytable",
     "label": "my table",
     "plural": "my tables"
}]
}

And that’s it! You have created your first table in MongoDB. Now you can GET and PUT or even DELETE entries in the table. If for any reason your table does not show up in the admin console interface you might want to clear the system cache from the config tab. Also, you will have to add the table to a user role to enable client side access through the REST API.

Flexible Structured Data

One of the cool aspects of MongoDB in particular and NoSQL in general is that you don’t have to worry too much about schema. You can store structured JSON documents and even add schema at a later date as needed. For example, you could create a document by POST-ing some JSON, like this:

http://myserver.com:80/api/v2/mongodb/_table/mytable

{
"resource": [{
     "amount": 300,
     "product": "whatever",
     "email": "[email protected]",
     "name": "value2"
}]
}

This POST will return a result that looks like this:

{
"resource":[{
     "_id":"561ff05d532c2e9b158b4567"
}]
}

MongoDB returns a unique identifier called “_id” for your document when it is created. The id can be used to update or delete the document at a later date. As you create documents, you can include new name-value pairs, or drop name-value pairs that were used in the past. By the way, you can create any number of tables at the same time with multiple objects in the “resources” array, above.

Simple Filtered Queries

Go ahead and create a couple of documents with some different name-value fields, and let’s try a filtered query. DreamFactory makes this really easy with a textual query language that is similar to SQL. Go to the API Docs tab and navigate to:

/mongodb/_table/{table_name}

You can search for any text field, or a number with inequalities, or even substrings in a text field. These are all valid examples you might try, below. Type these into the filter string interface, and click the “Try it out” button.

email LIKE %appleton%
example_field=whatever
amount>290 and amount<310

The result for my data using the first filter looks like this:

{
"resource":[{
     "_id": "561ff05d532c2e9b158b4567",
     "amount": 450,
     "product": "whatever",
     "email": "[email protected]",
     "name": "value2"
},
{
     "_id": "561ff4e0532c2efc7d8b4567",
     "amount": 300,
     "product": "whatever",
     "email": "[email protected]",
     "name": "value2"
}]
}

The call from a client application would be a simple GET like this:

http://myserver:80/api/v2/mongodb/_table/mytable?filter=email%20LIKE%20%25appleton%25 

The Big Leagues

Of course, you might already have a MongoDB database running in the cloud or on premises. In that case you can create a new service and hook up the database. MongoDB also has excellent products and services to support and scale your deployment as needed, but the bundled MongoDB database in DreamFactory 2.0 is a great way to start exploring NoSQL for your application needs.

Get started with DreamFactory with a free hosted DreamFactory development environment. Or, download and run it on the server, cloud, or desktop of your choice.

NoSQL SQL DreamFactory Silver

Weekly Digest