Mongo DB error: Invalid operator: $ search when performing text search

I created an index with this:

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

and I run the following in Robomongo:

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

and return this error:

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

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

+6
source share
1 answer

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" } }); 

This will give a result like:

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

Also, if you are using mongo 2.4, use the following command:

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

All Articles