【问题标题】:MongoDB full text search on string arrayMongoDB对字符串数组的全文搜索
【发布时间】:2015-06-06 22:46:25
【问题描述】:

所以我将 Node.js 与 MongoDB 一起用于我的 Web 应用程序。我在为我的模式创建文本索引和在数组中搜索文本时遇到了一些问题。我查看了 mongo 文档,但没有找到任何与此相关的具体内容。

我当前的实现在常规字符串值上成功搜索,但查询 [String] 中的文本匹配不返回任何内容。

这是我的 REST 调用:

...console.log("Query string: " + str); var qry = { "$text": { "$search": str } }; model.find(qry, function (err, results) {...

当我创建我的架构时:

var blah = new Schema({ foo : String, bar : [String],

...

blah.index({ foo: 'text', bar: 'text' });

任何查询都不会返回与bar 匹配的结果。 foo 内的查询字符串可以正常工作。

【问题讨论】:

  • 请发布您用于创建文本索引的语句。这与问题有关。

标签: node.js mongodb full-text-search


【解决方案1】:

仔细检查您是否在正确的集合上创建了正确的索引,并且正在向正确的集合发出查询。索引数组对我有用:

> 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 }

【讨论】:

    【解决方案2】:

    好吧,我终于想通了!事实证明,grunt serve 不会更新数据库中的索引。我只为“foo”创建了一个文本索引,当我将“bar”添加到索引时它没有更新。我必须在 mongo shell 中运行:

    db.dropDatabase()

    下次我运行它时,数据库被重新创建并设置了正确的索引。如果其他人遇到此问题,请尝试运行 db.getIndexes()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      相关资源
      最近更新 更多