Node.js with mongo(ose)
Upcoming SlideShare
Loading in...5
×
 

Node.js with mongo(ose)

on

  • 1,180 views

Using MongoDB from Node.js with mongoose

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

Statistics

Views

Total Views
1,180
Views on SlideShare
1,175
Embed Views
5

Actions

Likes
0
Downloads
10
Comments
0

1 Embed 5

https://twitter.com 5

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Node.js with mongo(ose) Node.js with mongo(ose) Presentation Transcript

  • Node.js with Mongo(ose) Gergely Nemeth
  • 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
  • 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 ;)
  • 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});
  • 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) })
  • 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) })
  • 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) { } );
  • 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
  • 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) })
  • $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" );