MongoDB is a NoSQL database or object store that is relatively easy to use.
mLab is a company which offers cloud based MongoDB hosting with a REST API that we can easily utilize.
Let's go through the Quick Start
Create a Single-node, Standard Line, Sandbox database with a name that makes sense for your application. Mine is called "aoactest".
Next you'll need a user to connect, click on the database and create one. Mine is "test", test; probably a bad idea.
Next, we'll want to create a "collection" and add some "documents" to start playing with the database. Mine is called "test", still probably a bad idea.
There are several node modules that are designed to work with mongo. I find the mongojs module to be the quickest to get started with.
To get started, as with any node module, we need to install it via npm:
npm install mongojs
Following that, we can use it in our applications and connect to our mLab hosted databases:
var mongojs = require('mongojs');
var db = mongojs("username:password@example.com:port/mydb", ["mycollection"]);
To insert a record into the database:
db.mycollection.save({"attribute_to_save":"value_to_save"}, function(err, saved) {
if( err || !saved ) console.log("Not saved");
else console.log("Saved");
});
To pull all records from the database:
db.mycollection.find({}, function(err, saved) {
if (err || !saved) {
console.log("No results");
}
else {
saved.forEach(function(record) {
console.log(record);
});
/* Alternatively you could loop through the records with a "for"
for (var i = 0; i < saved.length; i++) {
console.log(saved[i]);
}
*/
}
});
To search for records which match a query term:
db.mycollection.find({"attribute":"value_to_search_for"}, function(err, saved) {
if( err || !saved) console.log("No results");
else saved.forEach( function(record) {
console.log(record);
} );
});
Aside from a straight match as shown above, you can use a number of query operators that mongo supports. One of the most powerful is the regex or regular expression operator which allows you to use powerful regular expression string matching.
db.mycollection.find({"attribute": { $regex: /regular_expression_pattern/, $options: 'i' }}, function(err, saved) {
if( err || !saved) console.log("No results");
else saved.forEach( function(record) {
console.log(record);
} );
});
We can use a variable such as one that comes in from a query string to define our regular expression as follows:
app.get('/search', function(req, res) {
var query = new RegExp(req.query.q, 'i');
db2.posts.find({"keywords": query},
function(err, saved) {
if( err || !saved) console.log("No results");
else res.send(saved);
});
});
Here is the search form that would work with the above route:
<form method="GET" action="/search"> <input type='text' name="q"><input type="submit" name="submit" value="Search"> </form>More information about regular expressions:
To pull a specific record, we use a result's _id attribute:
db.mycollection.findOne({
_id: mongojs.ObjectId("58b654cf449473350d45047f")
}, function(err, record) {
console.log(record);
});