mongodb - Mongoose 全文搜索嵌套文档并填充选项

标签 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 执行全文搜索,但还需要一个完整的检查 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/

相关文章:

node.js - 根据数组元素查找对象,只返回匹配的数组元素?

javascript - 未处理的拒绝 MongoError : cannot connect to server in MongoDB

javascript - 在 for 循环中使用 Model.create() 和 save()

php - PHP全文搜索在elasticsearch

mysql联合与全文搜索错误

MongoDB 不将 block 移动到分片集群中的新分片

node.js - 如何在一台VPS服务器上运行多个独立的Loopback应用程序?

mongodb - 如何在Azure的DocumentDB中禁用SSL?

mongodb - Swagger项目创建命令行错误

android - 使用 Elasticsearch 进行搜索的最佳方法是什么(Via Server与Direct Java Client)?