2013-04-22 46 views
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(這一點,參數);} 沒有法「的文本搜索」

+0

您是否有使用文本索引的2.4以前版本的MongoDB? – WiredPrairie 2013-04-22 19:25:44

+0

沒有。在我決定使用文本索引後,我升級到2.4。 – Liatz 2013-04-22 19:35:12

+0

有可能我在第一次運行帶有文本索引的代碼時沒有停止mongod,但是我注意到我停止了mongod並從升級版本運行它。 – Liatz 2013-04-22 19:36:56

回答

0

這裏有一個同樣的問題 我從/貓鼬,文本的應用代碼解決它。 search/lib/index.js

YOURSCHEMA.statics.textSearch = function (search, o, cb) { 
    if ('function' == typeof o) cb = o, o = {}; 

    if ('function' != typeof cb) { 
     throw new TypeError('textSearch: callback is required'); 
    } 

    var model = this; 
    var lean = !! o.lean; 

    // mongodb commands require property order :(
    // text must be first 
    var cmd = {}; 
    cmd.text = o.text || this.collection.name; 

    cmd.search = search; 

    var keys = Object.keys(o); 
    var i = keys.length; 
    while (i--) { 
     var key = keys[i]; 
     switch (key) { 
     case 'text': 
      // fall through 
     case 'lean': 
      continue; 
     case 'filter': 
      cmd.filter = model.find(o.filter).cast(model); 
      break; 
     case 'project': 
      // cast and apply default schema field selection 
      var query = model.find().select(o.project); 
      query._applyPaths(); 
      var fields = query._castFields(query._fields); 
      if (fields instanceof Error) return cb(fields); 
      cmd.project = fields; 
      break; 
     default: 
      cmd[key] = o[key]; 
     } 
    } 

    this.db.db.command(cmd, function (err, res) { 
     if (err) return cb(err, res); 
     if (res.errmsg) return cb(new Error(res.errmsg)); 
     if (!lean && Array.isArray(res.results)) { 
     // convert results to documents 
     res.results.forEach(function (doc) { 
      if (!doc.obj) return; 
      var d = new model(undefined, undefined, true); 
      d.init(doc.obj); 
      doc.obj = d; 
     }) 
     } 
     cb(err, res); 
    }); 
    } 
} 

我的本地綱要文件

0

有一件事要仔細檢查是你應用的插件後,你不重寫statics財產。這是對我造成問題的原因。

mySchema.plugin(textSearch); 
mySchema.index({ name: 'text' }); 
... 
mySchema.statics = { ... }