【问题标题】:Mongo DB error: invalid operator: $search when doing $text searchMongodb错误:无效的运算符:进行$ text搜索时的$search
【发布时间】:2015-04-18 23:43:11
【问题描述】:

我已经用这个创建了一个索引:

db.MyCollection.ensureIndex({"field1": "text"})

我正在 Robomongo 中运行以下命令:

db.MyCollection.find({$text:{$search:"something"}})

并返回此错误:

error: { "$err" : "invalid operator: $search", "code" : 10068 }

文档似乎很清楚我使用了正确的语法:http://docs.mongodb.org/manual/reference/operator/query/text/。我正在使用 mongo 2.4.9 版。有谁知道这可能是什么问题?

【问题讨论】:

  • 方法在版本之间发生了变化。对于2.4.9版本的MongoDB,在documentation中更改选择的版本
  • 谢谢,把它写下来作为答案,我会接受的
  • 不需要。在relevant manual page 的顶部实际上有一个“new In 2.6”声明。
  • 我在这里看到了注释:docs.mongodb.org/v2.4/tutorial/enable-text-search 关于 2.4 版,所以我(错误地)假设手册中的 API 将存在于 2.4.9 中。我只是误读/跳过了 $text 运算符页面上的那个。我可以假设我是世界上唯一遇到这个问题的人(因为谷歌搜索“无效运算符:$search”没有返回任何相关内容),但我认为我的问题有朝一日可能会对某人有所帮助 - 也许它已经有我有一个赞成票。
  • 帮助了我。不知道为什么不愿意回答@NeilLunn

标签: mongodb


【解决方案1】:

在 mongo 2.6+ 中$text 的工作方式如下:

db.collection.insert({desc: "This is a string with text"});
db.collection.insert({desc:"This is a another string with Text"});
db.collection.insert({desc:"This is a another string with ext"});
db.collection.ensureIndex({"desc":"text"});
db.collection.find({
    $text:{
        $search:"text"
    }
}); 

这将给出输出:

{ "_id" : ObjectId("553277a608b85f33165bf3e0"),
 "desc" : "This is a another string with Text" }

{ "_id" : ObjectId("5532779f08b85f33165bf3df"), 
"desc" : "This is a string with text" }

另外,如果您使用的是 mongo 2.4 版,请使用以下内容:

 db.collection.ensureIndex({"desc":"text"});
 db.collection.runCommand( "desc", { search: "Text"})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2013-11-19
    • 2020-05-12
    • 2014-07-21
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多