提前致谢。 我有如下猫鼬模式:Mongoose Mongodb
var bookSchema = new mongoose.Schema({
name: { type: String, index: true, default: '' },
text: { type: String, index: true, default: '' },
date: { type: Date, default: Date.now },
price: { type: Number, default: 0 } });
bookSchema.index({text: 1, name: 1}, {unique: true});
我的问题是,当我搜索中指定的字段中的任何文本“文本”这是行不通的。可能是命名冲突,我必须到外地名称更改为文本以外的东西......
Book.find({ $text : { $search : 'mongoose is great' } })...
你得到了什么错误,你如何定义模型? –
我发现的一个大问题是我需要重新启动MongoDB服务器......(doh!)。我写了一个简短的函数bookSchema.pre('save',function(next){this.searchable = this.text +''+ this.name; next();});在一个新字段中包含所有可搜索的内容。 – Mendo
@Mendo添加索引时不需要重新启动MongoDB,也不需要为使用文本索引进行搜索创建单独的字段,并且标记为正确的答案也不正确。坏信息超载!你可能应该重新尝试一下。 – JohnnyHK