mongoose全文搜索嵌套文档和填充选项

时间:2015-03-15 16:30:33

标签: 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: [{
        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-full-text但我不知道如何索引嵌套文档如何包含嵌套文档(填充)。

如何使用mongoose的mongoose-full-text插件或者是否有其他选项?

1 个答案:

答案 0 :(得分:1)

经过一番研究,我终于得到了这个

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);
        });
    }
});