0
我有一個模式,並添加到它的方法:貓鼬:在查詢調用對象方法選擇
// define a schema
var animalSchema = new Schema({ name: String, type: String });
// assign a function to the "methods" object of our animalSchema
animalSchema.methods.slug = function() {
return type: this.type + '-' + this.name;
}
使用像這樣:
var Animal = mongoose.model('Animal', animalSchema);
var dog = new Animal({ type: 'dog', name: 'Bill });
dog.slug(); // 'dog-Bill'
我想在動物的查詢和獲取方法結果選擇:
Animal.find({type: 'dog'}).select('type name slug'); // [{type: 'dog', name: 'Bill', slug: 'dog-Bill'}]
我可以這樣做嗎?