MongoDB-Mongoose

From eBabel wiki
Jump to: navigation, search

MongoDB on Windows

If the MongoDB isn't running already, on Windows:

D:
cd mongodb\bin
mongod.exe --dbpath D:\mongodb\data

If you want to connect directly to the database in a separate console:

D:
cd mongodb\bin
mongo.exe

Useful commands:

show databases;
use test;
show collections;
db.entries.count();
db.entries.find();


Mean - MongoDB ExpressJS AngularJS NodeJS - Getting MongoDB and Mongoose working

Install Mongo on Ubuntu

To install Mongo on Ubunutu, this is official documented under:

  http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

However the following 'mongo_install.bash' script (from https://www.digitalocean.com/community/articles/how-to-install-mongodb-on-ubuntu-12-04 ), automates this nicely:

  apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
  echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | tee -a /etc/apt/sources.list.d/10gen.list
  apt-get -y update
  apt-get -y install mongodb-10gen

then just run:

  sudo bash ./mongo_install.bash
  ...
  Setting up mongodb-10gen (2.4.8) ...
  Adding system user `mongodb' (UID 106) ...
  Adding new user `mongodb' (UID 106) with group `nogroup' ...
  Not creating home directory `/home/mongodb'.
  Adding group `mongodb' (GID 111) ...
  Done.
  Adding user `mongodb' to group `mongodb' ...
  Adding user mongodb to group mongodb
  Done.
  mongodb start/running, process 1823
  vagrant@nodejs:/vagrant/efocus/expressjs$ ps -ef |grep mongodb
  mongodb   1823     1  5 12:38 ?        00:00:05 /usr/bin/mongod --config /etc/mongodb.conf


Mongo Start / Stop / Status

  vagrant@nodejs:/vagrant/efocus/expressjs$ sudo service mongodb status
  mongodb start/running, process 1823
  vagrant@nodejs:/vagrant/efocus/expressjs$ sudo service mongodb restart
  mongodb stop/waiting
  mongodb start/running, process 1860
  vagrant@nodejs:/vagrant/efocus/expressjs$ sudo service mongodb status
  mongodb start/running, process 1860

simple reads and writes via the mongo comand line interface =

  vagrant@nodejs:/vagrant$ mongo
  MongoDB shell version: 2.4.8
  connecting to: test
  > db.col1.insert({name:"mongo",type:"DB"})
  > db.col1.find()
  { "_id" : ObjectId("5293b236d7f79b07e8b0733f"), "name" : "mongo", "type" : "DB" }
  > show collections;
  col1
  system.indexes


Reads and writes via node mongoose to mongodb

Background useful tutorial

The following test script is from:

   http://blog.modulus.io/getting-started-with-mongoose

My first NodeJS/Mongoose test script =

So to write to the database, here is NodeJS/Mongoose test script

  app.get('/test_mongo', function (request, response) {
  
      var mongoose = require('mongoose');
  
      var db = mongoose.connection;
  
      db.on('error', console.error);
      db.once('open', function() {
        // Create your schemas and models here.
      });
  
      mongoose.connect('mongodb://localhost/test');
  
      var movieSchema = new mongoose.Schema({
        title: { type: String }
      , rating: String
      , releaseYear: Number
      , hasCreditCookie: Boolean
      });
  
      // Compile a 'Movie' model using the movieSchema as the structure.
      // Mongoose also creates a MongoDB collection called 'Movies' for these documents.
      var Movie = mongoose.model('Movie', movieSchema);
  
  
      var thor = new Movie({
            title: 'Thor'
          , rating: 'PG-13'
          , releaseYear: '2011'  // Notice the use of a String rather than a Number - Mongoose will automatically convert this for us.
          , hasCreditCookie: true
      });
  
      thor.save(function(err, thor) {
        if (err) return console.error(err);
        console.dir(thor);
      });
  
      response.writeHead(200, tigrAPI.getHead({
          bodyString: 'test_mongo2',
          cache: true    // Always cache since this shouldn't change too often and will be quite large.
      }));
      response.end('test_mongo2');
  });

simple call to NodeJS via curl

Now calling this very simply via curl:

  vagrant@nodejs:/vagrant/efocus/expressjs$ curl http://localhost:8000/test_mongo
  test_mongo2
  vagrant@nodejs:/vagrant/efocus/expressjs$

NodeJS console logs the creation of our movie object

in the NodeJS console we see object, we have created as expected for successful writes in the thor.save() function  :

  vagrant@nodejs:/vagrant/efocus/expressjs$ node app.js
  tigrAPI listening on port 8000
  { __v: 0,
    title: 'Thor',
    rating: 'PG-13',
    releaseYear: 2011,
    hasCreditCookie: true,
    _id: 5293b6bab62ffda406000001 }

Finally back in the database MongoDB

  > use test
  switched to db test
  > show collections;
  col1
  movies
  system.indexes
  > db.movies.find()
  { "title" : "Thor", "rating" : "PG-13", "releaseYear" : 2011, "hasCreditCookie" : true, "_id" : ObjectId("5293b6bab62ffda406000001"), "__v" : 0 }