Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo DB error: invalid operator: $search when doing $text search

Tags:

mongodb

I've created an index with this:

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

and I'm running the following in Robomongo:

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

and getting back this error:

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

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?

like image 447
B T Avatar asked Apr 22 '14 07:04

B T


People also ask

What is $text in MongoDB?

$search. string. A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase. See Behavior for more information on the field.

How do I search for text in MongoDB?

Use the $text query operator to perform text searches on a collection with a text index. $text will tokenize the search string using whitespace and most punctuation as delimiters, and perform a logical OR of all such tokens in the search string.

Does MongoDB support range query search?

Explanation: MongoDB supports search by field, range queries, regular expression searches.

How does search work in MongoDB?

MongoDB text search uses the Snowball stemming library to reduce words to an expected root form (or stem) based on common language rules. Algorithmic stemming provides a quick reduction, but languages have exceptions (such as irregular or contradicting verb conjugation patterns) that can affect accuracy.


1 Answers

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 gives output as :

{ "_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 version 2.4 then use following:

 db.collection.ensureIndex({"desc":"text"});
 db.collection.runCommand( "desc", { search: "Text"})
like image 50
Vishwas Avatar answered Sep 19 '22 13:09

Vishwas