1

当我运行 mongo db 我得到这个:

$ ./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', function (err, output) { ^ TypeError: Object function model() { Model.apply(this, arguments); } has no method 'textSearch'

4

2 回答 2

0

这里有同样的问题。我通过应用 /mongoose-text-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);
    });
  }
}

到我的本地架构文件

于 2013-09-10T13:17:32.180 回答
0

需要仔细检查的一件事是,您statics在应用插件后没有覆盖该属性。这就是造成我问题的原因。

mySchema.plugin(textSearch);
mySchema.index({ name: 'text' });
...
mySchema.statics = { ... }
于 2014-03-22T19:03:29.420 回答