From eBabel wiki
Jump to: navigation, search

MongoDB on Windows

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

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

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

cd mongodb\bin

Useful commands:

show databases;
use test;
show collections;

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

Install Mongo on Ubuntu

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

However the following 'mongo_install.bash' script (from ), automates this nicely:

  apt-key adv --keyserver --recv 7F0CEB10
  echo "deb 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) ...
  Adding user `mongodb' to group `mongodb' ...
  Adding user mongodb to group mongodb
  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;

Reads and writes via node mongoose to mongodb

Background useful tutorial

The following test script is from:

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.
      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) {
        if (err) return console.error(err);
      response.writeHead(200, tigrAPI.getHead({
          bodyString: 'test_mongo2',
          cache: true    // Always cache since this shouldn't change too often and will be quite large.

simple call to NodeJS via curl

Now calling this very simply via curl:

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

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 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;
  > db.movies.find()
  { "title" : "Thor", "rating" : "PG-13", "releaseYear" : 2011, "hasCreditCookie" : true, "_id" : ObjectId("5293b6bab62ffda406000001"), "__v" : 0 }