0

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.

Dhruvil Thaker
  • 1,950
  • 1
  • 15
  • 25

1 Answers1

1

Could you pass in a JSON array:

{$text: {$search: [ "potato",  "tomato" ] } }
morsor
  • 1,263
  • 14
  • 29
  • {$text: {$search: [ "potato", "tomato" ] } } is working well for searching multiple ingredients but what about exclusion? like {$text: {$search: [ "potato", "tomato" ,-beef,chicken,meat] } } – Dhruvil Thaker Aug 27 '15 at 13:14
  • Perhaps you can use $not -> https://docs.mongodb.org/manual/reference/operator/query/not/ – morsor Aug 27 '15 at 13:17
  • db.RecipeCollection.find({ $text: { $search:"[tomato potato]",$not:{["chicken beef"]},$language:"en"}).sort({num_ingredients:1} i have tried using $not but it is not working, may be exclusion would have some limitation up to one word only – Dhruvil Thaker Aug 30 '15 at 07:55
  • SOLUTION: for mulitple exclusion we can use like {$text: {$search: [ "potato", "tomato" ] -beef -chicken -meat } } – Dhruvil Thaker Aug 30 '15 at 08:36