Mongo DB错误:无效的运算符:执行$ text搜索时的$ search [英] Mongo DB error: invalid operator: $search when doing $text search

查看:70
本文介绍了Mongo DB错误:无效的运算符:执行$ text搜索时的$ search的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个索引:

I've created an index with this:

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

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

and I'm running the following in Robomongo:

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

并返回此错误:

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

文档似乎非常清楚我使用的语法正确: http://docs.mongodb.org/manual/reference/operator/query/text/.我正在使用2.4.9版的mongo.有人知道这里可能出什么问题吗?

The docs seem to be pretty clear that I'm using the correct syntax: http://docs.mongodb.org/manual/reference/operator/query/text/ . I'm using mongo version 2.4.9. Anyone know what the problem could be here?

推荐答案

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

In mongo 2.6+ $text works as follows:

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版,请使用以下内容:

Also if you are using mongo version 2.4 then use following:

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

这篇关于Mongo DB错误:无效的运算符:执行$ text搜索时的$ search的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆