Mongoose

From air
Revision as of 07:01, 23 October 2014 by Donsez (talk | contribs) (→‎Premiers)
Jump to navigation Jump to search

Paquet Node.js pour l'accès à un service MongoDB

Liens

Premiers

  1. suivez l'installation de MongoDB
  2. suivez l'installation de Node.js
  3. Exécutez le programme Node.js suivant
npm install mongoose
node blog.js
node findblog.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');
});

findblog.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("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);
});

Vérifiez depuis le shell mongo (./bon/mongo)

use blog
show collections
db.blogs.find()