1
當我運行蒙戈DB我得到這個:無法運行mongodb2.4文本搜索
$ ./mongo db
MongoDB shell version: 2.4.2
connecting to: db
Server has startup warnings:
** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Mon Apr 22 19:25:54.938 [initandlisten] Index { v: 1, key: { type: "text", color: "text", category_A: "text", category_B: "text", category_C: "text" }, ns: "db.items", name: "type_text_color_text_category_A_text_category_B_text_category_C_text", sparse: false, background: false } claims to be of type 'text', which is either invalid or did not exist before v2.4. See the upgrade section: http://dochub.mongodb.org/core/upgrade-2.4
> db.adminCommand({ setParameter : 1, textSearchEnabled : true })
{ "was" : false, "ok" : 1 }
> db.runCommand("text",{search:"le"})
{
"errmsg" : "exception: wrong type for field (text) 1 != 2",
"code" : 13111,
"ok" : 0
}
when I run the following code with nodejs I get -
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var Items = new Schema({
type : { type : String , default:""},
color : { type : [String] , default:""},
category_A : { type : String , default:""},
category_B : { type : String , default:""},
category_C : { type : String , default:""},
});
var textSearch = require("mongoose-text-search");
var ItemModel = mongoose.model('Item', Items);
Items.plugin(textSearch);
Items.index({
type :"text",
color :"text",
category_A :"text",
category_B :"text",
category_C :"text"
},
{
name: "best_match_index",
weights: {
type: 5,
color: 4,
}
}
)
ItemModel.textSearch('D', function (err, output) {
if (err)
console.log(err);
else
console.log(output)
})
運行此我得到: ItemModel.textSearch( 'd',函數(ERR,輸出){ ^ 類型錯誤:對象函數模型(){ Model.apply(這一點,參數);} 沒有法「的文本搜索」
您是否有使用文本索引的2.4以前版本的MongoDB? – WiredPrairie 2013-04-22 19:25:44
沒有。在我決定使用文本索引後,我升級到2.4。 – Liatz 2013-04-22 19:35:12
有可能我在第一次運行帶有文本索引的代碼時沒有停止mongod,但是我注意到我停止了mongod並從升級版本運行它。 – Liatz 2013-04-22 19:36:56