提前致謝。 我有如下貓鼬模式: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