Mongodb mongoose嵌套文档全文搜索和填充选项
我有这个型号Mongodb mongoose嵌套文档全文搜索和填充选项,mongodb,mongoose,full-text-search,Mongodb,Mongoose,Full Text Search,我有这个型号 /** * Item Schema */ var ItemSchema = new Schema( { content: { type: String, default: '', trim: true }, description: { type: String, trim: true }, hints: { type: Number, default: 0 }, status: { type
/**
* 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执行全文搜索,但还需要有一个完整填充的检查表文档(包括嵌套文档)
我尝试了,但不知道如何索引嵌套文档,也不知道如何包含嵌套文档(填充)
我该如何使用mongoose的mongoose全文插件,或者如果有其他选择?经过一些研究,我终于得到了这个答案
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
.populate()
不进行“连接”,它只通过在客户端执行附加查询作为“合并”来模拟这一点。要么将数据嵌入文档,要么使用文本字段搜索集合,然后通过$in
查询父文档以查找匹配的子文档引用。嗨,Neil,是的,我是说neasted;这就是猫鼬在文档中调用的方式(修饰文档)。不过,多亏了Drehamlett,我终于找到了答案。你们为什么要把这个类别填充到它自己里面呢?