Search code examples
mongodbrestfull-text-searchfull-text-indexing

multiple input and multiple exclusion using text search index in mongodb


i have collection of recipes in my mongoDb.in which i have created text search index on ingredients. so that i can search recipes according to the ingredients which i have.result should have recipes atleast ingredients which i pass. this is sample from my mongodb query

db.RecipeCollection.find({$text:{$search: "\"potato\" \"tomato\""}},{ score: { $meta: "textScore" } },{_id:0,ingredients:1,num_ingredients:1}).sort( { score:
 $meta: "textScore" },num_ingredients:1 } ).limit(20).pretty()

but problem is i want to pass ingredients through REST, how can i pass parameter like {$text:{$search: "\"potato\" \"tomato\""} in calling api.

recipes.find({ $text: { $search: req.params.ingredients,$language:"en" } }
            ,{ score: { $meta: "textScore" } }
            ,{_id :0,name:1,ingredients:1,url:1,image:1,num_ingredients:1})
            .sort({score: { $meta: "textScore" },num_ingredients:1})
            .limit(req.params.limit);

i have seen this question http://stackoverflow.com/questions/16902674/mongodb-text-search-and-multiple-search-words but i dont have enough reputation to write comment and ask there. and even in that it is not shown how can i pass multiple parameter.


Solution

  • Could you pass in a JSON array:

    {$text: {$search: [ "potato",  "tomato" ] } }