Difference between revisions of "Mongoose"
Jump to navigation
Jump to search
(Created page with "Liens * http://www.atinux.fr/2011/10/15/tutoriel-sur-mongoose-mongodb-avec-node-js/") |
|||
Line 1: | Line 1: | ||
+ | Paquet [[Node.js]] pour l'accès à un service [[MongoDB]] |
||
+ | |||
Liens |
Liens |
||
* http://www.atinux.fr/2011/10/15/tutoriel-sur-mongoose-mongodb-avec-node-js/ |
* http://www.atinux.fr/2011/10/15/tutoriel-sur-mongoose-mongodb-avec-node-js/ |
||
+ | * http://mongoosejs.com/docs/index.html |
||
+ | |||
+ | =Premiers= |
||
+ | # suivez l'installation de [[MongoDB]] |
||
+ | # suivez l'installation de [[Node.js]] |
||
+ | # Exécutez le programme Node.js suivant |
||
+ | |||
+ | <source lang="bash"> |
||
+ | npm install mongoose |
||
+ | node blog.js |
||
+ | </source> |
||
+ | |||
+ | |||
+ | '''blog.js''' |
||
+ | <source lang="javascript"> |
||
+ | |||
+ | var mongoose = require('mongoose'); |
||
+ | var Schema = mongoose.Schema; |
||
+ | |||
+ | mongoose.connect('mongodb://localhost/blog', function(err) { |
||
+ | if (err) { throw err; } |
||
+ | }); |
||
+ | |||
+ | console.log("Create Schema"); |
||
+ | |||
+ | var blogSchema = new Schema({ |
||
+ | title: String, |
||
+ | author: { type : String, match: /^[a-zA-Z0-9-_]+$/ }, |
||
+ | body: String, |
||
+ | url: String, |
||
+ | comments: [{ body: String, date: Date }], |
||
+ | date: { type: Date, default: Date.now }, |
||
+ | hidden: Boolean, |
||
+ | meta: { |
||
+ | votes: Number, |
||
+ | favs: Number |
||
+ | } |
||
+ | }); |
||
+ | |||
+ | // assign a function to the "statics" object of our blogSchema |
||
+ | blogSchema.statics.findByAuthor = function (author, cb) { |
||
+ | this.find({ author: new RegExp(author, 'i') }, cb); |
||
+ | } |
||
+ | |||
+ | // blogSchema.set('autoIndex', false); |
||
+ | |||
+ | console.log("Create Model"); |
||
+ | |||
+ | var Blog = mongoose.model('Blog', blogSchema); |
||
+ | |||
+ | console.log("Create Blogs"); |
||
+ | |||
+ | |||
+ | var b1 = new Blog({ |
||
+ | title: 'FablabAIR', |
||
+ | author: 'dd', |
||
+ | body: 'AIR is a fablab (fabrication laboratory) for engineering students in Grenoble. AIR helps them to innovate, invent and make ambient intelligence (AmI) products.', |
||
+ | url: 'http://air.imag.fr', |
||
+ | }); |
||
+ | b1.save(function (err) { |
||
+ | if (err) { throw err; } |
||
+ | console.log('Successful new blog'); |
||
+ | }); |
||
+ | |||
+ | var b2 = new Blog({ |
||
+ | title: 'FabMSTIC', |
||
+ | author: 'jm', |
||
+ | body: 'AIR is a fablab (fabrication laboratory) for researchs and eduction in computer sciences and mathematics', |
||
+ | url: 'http://fabmstic.liglab.fr', |
||
+ | }); |
||
+ | b2.save(function (err) { |
||
+ | if (err) { throw err; } |
||
+ | console.log('Successful new blog'); |
||
+ | }); |
||
+ | |||
+ | console.log("Find Blogs"); |
||
+ | |||
+ | Blog.find(function (err, blogs) { |
||
+ | if (err) return console.error(err); |
||
+ | console.log(blogs) |
||
+ | }); |
||
+ | |||
+ | Blog.findByAuthor('dd', function (err, blogs) { |
||
+ | if (err) return console.error(err); |
||
+ | console.log(blogs); |
||
+ | }); |
||
+ | |||
+ | |||
+ | |||
+ | </source> |
Revision as of 22:16, 22 October 2014
Paquet Node.js pour l'accès à un service MongoDB
Liens
- http://www.atinux.fr/2011/10/15/tutoriel-sur-mongoose-mongodb-avec-node-js/
- http://mongoosejs.com/docs/index.html
Premiers
- suivez l'installation de MongoDB
- suivez l'installation de Node.js
- Exécutez le programme Node.js suivant
npm install mongoose
node blog.js
blog.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/blog', function(err) {
if (err) { throw err; }
});
console.log("Create Schema");
var blogSchema = new Schema({
title: String,
author: { type : String, match: /^[a-zA-Z0-9-_]+$/ },
body: String,
url: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
// assign a function to the "statics" object of our blogSchema
blogSchema.statics.findByAuthor = function (author, cb) {
this.find({ author: new RegExp(author, 'i') }, cb);
}
// blogSchema.set('autoIndex', false);
console.log("Create Model");
var Blog = mongoose.model('Blog', blogSchema);
console.log("Create Blogs");
var b1 = new Blog({
title: 'FablabAIR',
author: 'dd',
body: 'AIR is a fablab (fabrication laboratory) for engineering students in Grenoble. AIR helps them to innovate, invent and make ambient intelligence (AmI) products.',
url: 'http://air.imag.fr',
});
b1.save(function (err) {
if (err) { throw err; }
console.log('Successful new blog');
});
var b2 = new Blog({
title: 'FabMSTIC',
author: 'jm',
body: 'AIR is a fablab (fabrication laboratory) for researchs and eduction in computer sciences and mathematics',
url: 'http://fabmstic.liglab.fr',
});
b2.save(function (err) {
if (err) { throw err; }
console.log('Successful new blog');
});
console.log("Find Blogs");
Blog.find(function (err, blogs) {
if (err) return console.error(err);
console.log(blogs)
});
Blog.findByAuthor('dd', function (err, blogs) {
if (err) return console.error(err);
console.log(blogs);
});