Node.js with mongo(ose)

1,195 views

Published on

Using MongoDB from Node.js with mongoose
including: getting started, basic queries, aggregation, populate, mapreduce and $where

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,195
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Node.js with mongo(ose)

  1. 1. Node.js with Mongo(ose) Gergely Nemeth
  2. 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. 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 ;)
  4. 4. Basic queries Gergely Nemeth ● use testDb; ● db.testDb.insert({co:’apple’, cnt: 3}); ● db.testDb.insert({co:’lmi’, cnt: 1}); ● db.testDb.find(); ● db.testDb.findOne(); ● db.testDb.find().limit(2); ● db.testDb.find().sort({cnt:1});
  5. 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. 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) })
  7. 7. Aggregation Gergely Nemeth ● available since version 2.2 ● unix-like pipelines for: $project, $match, $limit, $skip, $unwind, $group, $sort, $geoNear User.aggregate([ { $match: {someBoolVal: true} }, { $limit : 10 }, { $project: { fid: "$fid" }}, ], function(err, data) { } );
  8. 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. 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. 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" );

×