Database
, Test and
Grunt
mongoDB (from "humongous") is a cross-platform document-oriented database.

- 2007
- NoSQL
- GNU AGP License
No Schema?!
- Documents (JSON Objects)
- No relations
But, but...
- Indices make it fast
- BSON (Binary JSON)
RDBMS
Database
Table
Column
Row
Primary Key
MongoDB
Database
Collection
Field
Document
Primary Key (default: _id)
CRUD Operations
> mongod
all output going to:
/usr/local/var/log/mongodb/mongo.log> mongo
MongoDB shell version: 2.4.9
connecting to: test
>Creating
- use *database_name*
- show dbs
- db.createCollection(*coll_name*)
- show collections
Inserting
- db.my_collection.insert({'age': 26, 'name': 'gustav'})
Reading
- db.my_collection.find()
- db.my_collection.find().pretty()
- db.my_collection.find({'age': 26})
- db.my_collection.find({'age': {$gt: 24}})
Updating
- db.my_collection.update({'age': 26}, {'age': 27})
- db.my_collection.update({'age': 26}, {$set: {'age': 27}})
Deleting
- db.my_collection.remove({'age': 25})
Advanced Operations

Find with projection
- db.my_collection.find({'age': 23}, {'age': 1, '_id': 0})
Limiting
- db.my_collection.find().limit(2)
- db.my_collection.find().limit(2).skip(3)
- db.my_collection.findOne()
Indexing
- db.my_collection.ensureIndex({'coffee': 1})
- db.my_collection.getIndexSpecs()
- db.my_collection.dropIndex(index_name)
To avoid full collection scan for a query, ensure indices of fields you are likely to use
Aggregation framework



Tests
Functions
User interface
Mocha
Mocha is a feature-rich JavaScript test framework running on node.js and the browser, making asynchronous testing simple and fun.
Install Mocha
$ npm install -g mochaMocha
describe('###Test Description###', function(){
describe('###Detailed Test Description###', function(){
it('should ###do stuff###', function(){
//assert stuff using
//third party assert library
})
})
})test/test.js
$ mochaMocha async
describe('###Test Description###', function(){
describe('###Detailed Test Description###', function(){
it('should ###do stuff###', function(done){
//assert stuff using
//third party assert library
done();
})
})
})Mocha + assertion lib
-
should.js
-
expect.js
-
better-assert
-
chai
Chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

Expect
var expect = require('chai').expect;
var request = require('request');
describe('Complete webapp', function(){
before(function(done){
request('http://localhost:3000/logout', function(err, res, body){
expect(err).to.not.exist;
done();
});
});
it('should load the start page successfully', function(done){
request('http://localhost:3000', function(err, res, body){
expect(err).to.not.exist;
expect(res.statusCode).to.equal(200);
done();
});
});
});
new Nightmare()
.goto('http://yahoo.com')
.type('input[title="Search"]', 'github nightmare')
.click('.searchsubmit')
.run();
The JavaScript Task Runner
Install Grunt
$ npm install -g grunt-cli
$ npm install grunt --save-devGruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
files: ['package.json', 'Gruntfile.js', 'routes/*.js'],
options: {
// options here to override JSHint defaults
globals: {
jQuery: true,
console: true,
module: true,
document: true
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('test', ['jshint']);
}Last time...
- Socket.IO
- ...beyond the server
Node.JS #4
By Gustav Jorlöv
Node.JS #4
- 739