Vous êtes sur la page 1sur 14

MongoDB, Mongoose

Zdeněk Rejda @ZdenekRejda


MongoDB
● Fast
● Scalable
● Tunable consistency
● No tables
● No schema
● No SQL
● Open source (C++)
● Drivers - python, C#,
javascript,....
● Community
Document
● stored as BSON - Binary JSON
● max 16MB per document
● each document has “_id”
● embedded documents
var document = {
_id: ObjectId("5099803df3f4948bd2f98391"),
name: { first: "Alan", last: "Turing" },
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}
Document - data types
● ObjectID
● Numbers (Integer, Long, Double)
● String
var document = {
● Date
_id: ObjectId("5099803df3f4948bd2f98391"),
● Boolean .
● Array .
● Objects .
● Null }

ObjectID - 12 bytes where:


● 4 bytes - UNIX timestamp
● 3 bytes - machine identifier
● 2 bytes - process ID
● 3 bytes - counter (started with random number)
Replica set
● Data durability
● Consistency
● Scalability
Storage
● Primary component
● Variety of engines
● Chosing of suitable will affects
application performance
Main MongoDB modules
● mongod - daemon (service, on demand)
● mongo - shell
● mongoimport
● mongoexport
What is Mongoose?
● Object document mapper (ODM) for Node.js
● Huge community
● Brings advanced schema-based features:
● Async validation
● Casting
● Pseudo-joins
● Rich query builder
● Defaults
● Life-cycle management
Mongoose Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var blogSchema = new Schema({


title: {type: String, required: true} // with validation!
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
Mongoose Schema - types
● String
● Number
● Date
● Buffer
● Boolean
● Mixed
● ObjectId
● Array
Mongoose Model
● Constructor var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
● Compiled from schema
● Document = instance of var Cat = mongoose.model('Cat', { name: String });

model !
var kitty = new Cat({ name: 'Zildjian' });
● Allows manipulations
kitty.save(function (err) {
above collections -
if (err) {
query, update, remove.. console.log(err);
} else {
console.log('meow');
}
});
Mongoose query
var Person = mongoose.model('Person', yourSchema); // With a JSON doc // Using query builder
Person. Person.
// find each person with a last name matching 'Ghost', find({ find({ occupation: /host/ }).
selecting the `name` and `occupation` fields
occupation: /host/, where('name.last').equals('Ghost').
Person.findOne({ 'name.last': 'Ghost' }, function (err, person)
'name.last': 'Ghost', where('age').gt(17).lt(66).
{
age: { $gt: 17, $lt: 66 }, where('likes').in(['vaporizing', 'talking']).
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.
likes: { $in: ['vaporizing', 'talking'] } limit(10).
name.last, person.occupation) }). sort('-occupation').
}) limit(10). select('name occupation').
sort({ occupation: -1 }). exec(callback);
select({ name: 1, occupation: 1 }).
exec(callback);
Mongoose pseudo joins
var mongoose = require('mongoose'),
ObjectId = mongoose.Schema.Types.ObjectId;

var moduleLiftModel = function () {

var moduleLiftSchema = mongoose.Schema({


lift_id : { type: ObjectId, ref: 'Lift' },
module_id : { type: ObjectId, ref: 'Module' },
link_on: Date
}, { collection: 'modulelift' });

return mongoose.model('ModuleLift', moduleLiftSchema);


};

module.exports = new moduleLiftModel();


Example…..
● node.js
● express.js
● react.js
● mongoose.js

Vous aimerez peut-être aussi