RFID Enabled Fish Tracking

Documentation of the HI-Tag Web Database Interface

Back Index Accounts Heroku MongoDB Node.js

MongoDB Documentation

Installing MongoDB

Follow the guide here

Here is a reasonably good MongoDB tutorial.

Using MongoDB with Node.js

npm install mongoose

It is quite easy to use mongoose. I suggest following the docs here.

Cloud-based Storage with MLab

MLab (formerly MongoLab) is a cloud-based provider for MongoDB.

You get 500 MB for free when you sign up, which is quite nice of them.

Creating a DB Instance

  1. Sign up at the mLab website given above.

  2. After you sign up, click “+ create new”. This redirects you the creation page.

  3. In your plans, select “single-node”, and “sandbox”. Supply the name of the database and create.

  4. In order to use the database, a database user is required. Click your database to go to management page.

  5. Select “Users” in the tab below and click “+ Add database user”.

  6. Enter the desired credentials and save it somewhere so that you can remember it later.

Connecting to the DB Instance

MLab Website has instructions too, but here is a copy of it:

(db endpoint should look like some_string.mlab.com:port, where port is a number. You should be able to find it at the website.)

  • Mongo shell:

    mongo <dbendpoint>/<dbname> -u <dbuser> -p <dbpassword>
    
  • Mongoose:

    mongoose.connect(mongodb://<dbuser>:<dbpassword>@<dbendpoint>/<dbname>)
    

Support for Complex File Types with GridFS

GridFS allows you to store files such as images into MongoDB.

Installing GridFS

  • With NPM:

    npm install gridfs-stream
    

Using GridFS

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB.

Documentation on accessing GridFS through the shell is in the link above.

  • Connecting (with Mongoose)

    
    var mongoose = require('mongoose');
    mongoose.connect('YOUR DATABASE ENDPOINT');
    
    var gridfs = require('gridfs-stream')
    gridfs.mongo = mongoose.mongo;
    
    var con = mongoose.connection;
    var gfs = null;
    con.once('open', function () {
        console.log('MONGODB Connection established');
        //initialize gridfs
        gfs = gridfs(con.db);
    });
    
  • Writing to GridFS

    function gfsWrite(filename){
       var writestream = gfs.createWriteStream({
          filename: filename
       });
       return fs.createReadstream(filename).pipe(writestream);
    }
    
  • Reading from GridFS

    function gfsRead(filename){
       var readstream = gfs.createdReadStream({
          filename: filename 
       });
       return readstream.pipe(writestream);
    }
    
  • Deleting from GridFS

    function gfsDelete(filename){
       gfs.remove({filename : filename},function(err){
           if(err){
               console.log(err);
           }else{
               doc.remove();
               console.log("REMOVE SUCCESS");
           }
       });
       
    }