2. What is MongoDB?
Gergely Nemeth
● document-oriented database (NoSQL)
● ad hoc queries
● indexing (any field)
● replication, sharding, load balancing
● aggregation/mapreduce
● javascript execution (V8)
● HTTP interface
3. Up and running with Mongo
Gergely Nemeth
1. download from: http://www.mongodb.org/downloads
2. start a mongod instance with: mongod
3. connect to mongod with: mongo
4. let’s get rolling ;)
5. Mongoose
Gergely Nemeth
● mongodb object modeling for node.js
● schema-based
npm install mongoose
//app.js
var mongoose = require('mongoose'); mongoose.connect('mongodb:
//localhost/test');
var myFirstSchema = mongoose.Schema({ name: String });
var myFirstSchema = mongoose.model('myFirstSchema', myFirstSchema);
var myFirstSchemaObject = new myFirstSchema({ name: 'mongo' });
myFirstSchemaObject.save(function(err){/*error handling*/});
myFirstSchema.find(function (err, results) {
if (err) // TODO handle err
console.log(results)
})
6. Populate
Gergely Nemeth
● populates document references
● MongoDB doesn’t have ‘Populate’, it comes from
Mongoose, so additional queries will be ran in the
background
Page.findOne({name:’My little pony’})
.populate(‘author’)
.exec(function(err, data) {
console.log(data)
})
8. Aggregation advices
Gergely Nemeth
● $match as soon as possible
● operators $match, $sort, $limit, $skip can
take advantage of indexes, so use them!
● aggregations works well with sharded configurations
● if an aggregation operations uses more than 10% of
SYS RAM, it will produce an error
● output of an agg. operation cannot be bigger than
16MB, if does, it will produce an error
9. MapReduce
Gergely Nemeth
● Map(): sorting and filtering
● Reduce(): summary operation
● in Mongo Map and Reduce are both JavaScript
functions (ran by V8)
var o = {};
o.map = function () { emit(this.name, 1) }
o.reduce = function (k, vals) { return vals.length }
User.mapReduce(o, function (err, results) {
console.log(results)
})
10. $where
Gergely Nemeth
● use only when all hell breaks loose, and the query
cannot be express with other operators
● cannot take advantage of indexes!
● requires a table scan!
db.myCollection.find( "this.credits == this.debits
|| this.credits > this.debits" );