我有这个模型
/**
* Item Schema
*/
var ItemSchema = new Schema( {
content: {
type: String,
default: '',
trim: true
},
description: {
type: String,
trim: true
},
hints: {
type: Number,
default: 0
},
status: {
type: [{
type: String,
enum: [ 'draft', 'published', 'vetoed' ]
}],
default: 'draft'
}
});
/**
* Section Schema
*/
var SectionSchema = new Schema( {
name: {
type: String,
default: '',
required: 'Please fill Checklist name',
trim: true
},
description: {
type: String,
trim: true
},
hints: {
type: Number,
default: 0
},
status: {
type: [{
type: String,
enum: [ 'draft', 'published', 'vetoed' ]
}],
default: 'draft'
},
items : [ItemSchema]
});
/**
* Checklist Schema
*/
var ChecklistSchema = new Schema( {
name: {
type: String,
default: '',
required: 'Please fill Checklist name',
trim: true
},
description: {
type: String,
trim: true
},
content: {
type: String,
default: '',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
reference: {
type: String,
trim: true
},
hints: {
type: Number,
default: 0
},
language: {
type: String,
default: 'en'
},
category: {
type: Schema.ObjectId,
ref: 'Category'
},
status: {
type: [{
type: String,
enum: [ 'draft', 'published', 'vetoed' ]
}],
default: 'draft'
},
sections: [SectionSchema]
});
我需要对 checklist.name、checklist.description、checklist.sections.name、checklist.sections.description、checklist.sections.items.content 执行全文搜索,但还需要一个完整的检查 list 文档(包括嵌套文档)。
我试过mongoose-full-text但我不知道如何索引嵌套文档,也不知道如何包含嵌套文档(填充)。
如何使用 mongoose 的 mongoose-full-text 插件或者是否有其他选项来做到这一点?
最佳答案
经过一番研究,我终于明白了
Checklist.textSearch(query, function (err, checklists) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
var iter = function (result, callback){
Category.populate(result.obj, { path: 'category', select: 'name' }, function(err, catCklst){
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
User.populate(catCklst, { path: 'user', select: 'displayName'}, callback);
}
});
};
async.each(checklists.results, iter, function(err){
console.log(JSON.stringify(checklists));
res.json(checklists);
});
}
});
关于mongodb - Mongoose 全文搜索嵌套文档并填充选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063130/