当我运行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)
})运行这个I得到: ItemModel.textSearch('D',function (err,output) {^ TypeError:对象函数模型(){ Model.apply( this,arguments);}没有方法'textSearch‘
发布于 2013-09-10 21:17:32
这里也有同样的问题。我通过应用/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);
});
}
}添加到我的本地架构文件
发布于 2014-03-23 03:03:29
要仔细检查的一件事是,在应用插件之后,您没有覆盖statics属性。这就是给我带来问题的原因。
mySchema.plugin(textSearch);
mySchema.index({ name: 'text' });
...
mySchema.statics = { ... }https://stackoverflow.com/questions/16152490
复制相似问题