Mongo DB错误:无效的运算符:$ search执行$ text搜索

B T*_*B T 6 mongodb

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

db.MyCollection.ensureIndex({"field1": "text"})
Run Code Online (Sandbox Code Playgroud)

我在Robomongo运行以下内容:

db.MyCollection.find({$text:{$search:"something"}})
Run Code Online (Sandbox Code Playgroud)

并收回此错误:

error: { "$err" : "invalid operator: $search", "code" : 10068 }
Run Code Online (Sandbox Code Playgroud)

文档似乎非常明确,我使用了正确的语法:http://docs.mongodb.org/manual/reference/operator/query/text/.我正在使用mongo版本2.4.9.有谁知道问题可能在这里?

Vis*_*was 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"
    }
}); 
Run Code Online (Sandbox Code Playgroud)

这将给出输出:

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

{ "_id" : ObjectId("5532779f08b85f33165bf3df"), 
"desc" : "This is a string with text" }
Run Code Online (Sandbox Code Playgroud)

另外,如果您使用 mongo 版本 2.4,则使用以下内容:

 db.collection.ensureIndex({"desc":"text"});
 db.collection.runCommand( "desc", { search: "Text"})
Run Code Online (Sandbox Code Playgroud)