Home > Enterprise >  Finding best result from mongodb databse
Finding best result from mongodb databse

Time:12-11

I am making a Recipe website project.

I have a collection named Recipe in my Mongodb atlas database.

In this collection, I have various documents of recipes. This documents contains name, timeToCook, proteinsContent, etc fields.

It also contains Keywords field, which consists of array of words - which depicts the type of recipe.

Now, in this website, I want to add one functionality of Recommendation.

When user will open a particular recipe, the Keywords of this Recipe will go to backend. And I want to write a mongodb query in backend, which will find for recipes in collection which have more Keywords match as per the selected recipe.

Eg. If Recipe is "RAISINS COOKIES" and Keywords for it are-Dessert,Snacks,Oven,Easy. So, I want to find the recipes in collections which will have Keywords which will match most of the Keywords given above.

I am using Mongodb and Nodejs.

CodePudding user response:

You could use a simplistic recommender system that just examines the "$size" of the "$setIntersection" of the "Keywords".

db.Recipe.aggregate([
  {
    "$set": {
      "interSize": {
        "$size": {
          "$setIntersection": [
            "$Keywords",
            // your comparison Keywords go here
            ["Dessert", "Snacks", "Oven", "Easy"]
          ]
        }
      }
    }
  },
  {"$sort": {"interSize": -1}},
  {"$limit": 5}
])

Try something similar on mongoplayground.net.

  • Related