在string数组上进行MongoDB全文search
所以我使用Node.js和MongoDB作为我的web应用程序。 我在为我的模式创build文本索引并search数组中的文本时遇到了一些麻烦。 我已经看过mongo文档,但没有find任何与此有关的具体内容。
我当前的实现在常规string值上成功search,但在[string]中查询文本匹配不会返回任何内容。
这是我的REST电话:
...console.log("Query string: " + str); var qry = { "$text": { "$search": str } }; model.find(qry, function (err, results) {...
当我创build我的模式时:
var blah = new Schema({ foo : String, bar : [String],
…
blah.index({ foo: 'text', bar: 'text' });
任何查询都不会返回符合bar
的结果。 foo
某个查询string正常工作。
仔细检查您是否在正确的集合上创build了正确的索引,并且正在向正确的集合发出查询。 索引数组适用于我:
> db.test.drop() > db.test.insert({ "_id" : 0, "a" : "dogs are good" }) > db.test.insert({ "_id" : 1, "a" : "I like dogs", "b" : ["where's my dog?", "here, have a cat"] }) > db.test.insert({ "_id" : 2, "b" : ["she borrowed my dog", "my frogs are croaking"] }) > db.test.ensureIndex({ "a" : "text", "b" : "text" }) > db.test.find({ "$text" : { "$search" : "dogs" } }, { "_id" : 1 }) { "_id" : 0 } { "_id" : 2 } { "_id" : 1 }
好吧,我终于明白了! 事实certificate,grunt服务不会更新数据库中的索引。 我只为“foo”创build了一个文本索引,并且在向索引添加“bar”时没有更新。 我不得不运行在蒙戈壳:
db.dropDatabase()
我下次运行它时,数据库被重新创build,并设置了适当的索引。 如果其他人遇到此问题,请尝试运行db.getIndexes()
。