You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mongodb-lectures/06. ADVANCED_MONGOOSE.md

3.8 KiB

Advanced Mongoose

Lesson Objectives

  1. Explain Methods
  2. Explain Statics
  3. Explain Indexes
  4. Explain Populate
  5. Explain SubDocuments
  6. Explain Middleware

Explain Methods

articleSchema.methods.longTitle = function(){
	return this.author + ": " + this.title;
}
console.log(article.longTitle());

Explain Statics

articleSchema.statics.search = function (name, cb) {
	return this.find({ 
		$or : [ 
			{ title: new RegExp(name, 'i') },
			{ author: new RegExp(name, 'i') }
		]
	}, cb);
}
Article.search('Some', function(err, data){
	console.log(data);
});

Explain SubDocuments

var authorSchema = new Schema({
  name: { type: String },
  articles: [articleSchema]
});

var Article = mongoose.model('Article', articleSchema);
var Author = mongoose.model('Author', authorSchema);

var matt = new Author({name: 'Matt'});
var article_id;
matt.save(function(){
  var article1 = new Article({title:'Awesome Title', author: matt._id});
  article1.save(function(){
    article_id = article1._id;
    matt.articles.push(article1);
    matt.save(showAll);
  });
});
var showAll = function(err, author){
  Author.findOne({}, function(err, result){
    console.log(result.articles.id(article_id));
    mongoose.connection.close();
  });
};
  1. parent.children.create({ name: 'Aaron' });
  2. parent.children.id(id).remove();
  3. var parentSchema = new Schema({ children: [{ name: 'string' }] })

Explain Populate

var mongoose = require('mongoose');
mongoose.set('debug', false);
mongoose.connect('mongodb://localhost:27017/test');
var Schema = mongoose.Schema;

var articleSchema = new Schema({
  title: { type: String },
  author: { type: Schema.Types.ObjectId, ref: 'Author' }
});

var authorSchema = new Schema({
  name: { type: String }
});

var Article = mongoose.model('Article', articleSchema);
var Author = mongoose.model('Author', authorSchema);

var matt = new Author({name: 'Matt'});
matt.save(function(){
  var article1 = new Article({title:'Awesome Title', author: matt._id});
  article1.save(function(){
    showAll();
  });
});

var showAll = function(){
  Article.find().populate('author').exec(function(error, article){
    console.log(article);
    mongoose.connection.close();
  })
};
  1. arrays of refs and updating refs with obj
var authorSchema = new Schema({
  name: { type: String },
  articles: [{type: Schema.Types.ObjectId, ref: 'Articles'}]
});

var Article = mongoose.model('Article', articleSchema);
var Author = mongoose.model('Author', authorSchema);

var matt = new Author({name: 'Matt'});
var article_id;
matt.save(function(){
  var article1 = new Article({title:'Awesome Title', author: matt._id});
  article1.save(function(){
    article_id = article1._id;
    matt.articles.push(article1);
    matt.save(showAll);
  });
});

var showAll = function(err, author){
  Author.find().populate('articles').exec(function(err, authors){
    console.log(authors);
    mongoose.connection.close();
  });
};

Explain Middleware

articleSchema.pre('save', function(next){
  console.log(this);
  console.log('saving to backup database');
  next();
});

async

articleSchema.pre('save', true, function(next){
  console.log(this);
  console.log('saving to backup database');
  next();
  doAsync(done);
});

post

articleSchema.post('save', function(next){
  console.log('saving complete');
});

##Explain Indexes

author: { type: String, required: true, index: true }, //in schema
{ autoIndex : false } // at end of schema
articleSchema.index({title:1, author:-1});
articleSchema.index({title:'text', author:'text'});
Article.ensureIndexes(function(){...});

Article.find({ $text : { $search: 'Awesome'} }, function(err, results){
	console.log(results);
	mongoose.connection.close();
});