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

171 lines
3.8 KiB

# Advanced Mongoose
## Lesson Objectives
1. Explain Methods
1. Explain Statics
1. Explain Indexes
1. Explain Populate
1. Explain SubDocuments
1. Explain Middleware
## Explain Methods
```javascript
articleSchema.methods.longTitle = function(){
return this.author + ": " + this.title;
}
console.log(article.longTitle());
```
## Explain Statics
```javascript
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
```javascript
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' });`
1. `parent.children.id(id).remove();`
1. `var parentSchema = new Schema({ children: [{ name: 'string' }] })`
## Explain Populate
```javascript
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
```javascript
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
```javascript
articleSchema.pre('save', function(next){
console.log(this);
console.log('saving to backup database');
next();
});
```
async
```javascript
articleSchema.pre('save', true, function(next){
console.log(this);
console.log('saving to backup database');
next();
doAsync(done);
});
```
post
```javascript
articleSchema.post('save', function(next){
console.log('saving complete');
});
```
##Explain Indexes
```javascript
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();
});
```