Flame_Phoenix Flame_Phoenix - 1 month ago 14
Javascript Question

Mongodb "Can't canonicalize query" error

Objective



Find what is wrong with my MongoDB query

Background



I have a drinks collection, where I enter data like the following:

db.smoothies.insert({
name: "Orange Banana",
ingredients: [], //list of ingredients
preparation: "Mix everything and have fun!",
Source: "Super Smoothies, p. 142"
})


I am trying to create a search by name functionality, and to achieve it I have created a
$text
index, according to the documentation:
https://docs.mongodb.com/manual/core/index-text/

So I used the following:

db.smoothies.createIndex({name: 'text'})


Now, when I make a simple
find
query like the following one, it works:

db.smoothies.find({ $text: { $search: "Orange" } })


But when i try to define case sensitivity, it fails:

db.smoothies.find( { $text: { $search: "orange", $caseSensitive: true } } )


And I get the following error:

error: {
"$err" : "Can't canonicalize query: BadValue extra fields in $text",
"code" : 17287
}


Question



I have taken this example from the documentation, and I can't understand why it fails. Can someone help me figure it out?

Answer

you need mongodb version 3.2 at least to use this query

have a look at the 2.6 documentation for text index :

https://docs.mongodb.com/v2.6/core/index-text/