1

My project is a recipe app where people view recipes and rate them. One part of the functionality I have is for people to be able to save the recipes they like as favourites. I am considering one approach to accomplish this, but I am worried about how it might impact performance.

My mongoose model structure for each user document is this:

{
    "_id" : ObjectId("594180e3cd48222f48eead43"),
    "username" : "T-Dot",
    "usersFavouriteRecipes" : [ 
        "594184662a932a1d287aac8a"
    ],
    "tokens" : [],
    "userId" : "520a2685-df58-4118-a928-51138ab81bb5",
    "__v" : 0
}

In my user document, usersFavouriteRecipes is an ordinary array. Every time a user adds a recipe to his favourites, I am considering pushing a string that mirrors the _id of the recipe into the usersFavouriteRecipes array.

Whenever I do so, it should look something like this:

    User.update({_id: "594180e3cd48222f48eead43"}, 
{$addToSet: {usersFavouriteRecipes: req.params.recipeId}}, 
(err, doc) => {
         if (err) console.log(err);
         console.log(doc);
         console.log('updating usersFavouriteRecipes....' + user);
                        });

However, I am not sure if this is the best approach in terms of performance. I've heard people say that inserting data into embedded arrays in MongoDB/Mongoose is costly to performance.

If I go with this route, I may use the strings in the array to get the documents from the Recipe collection whenever the user wants to see his favourited recipes. And hence, this approach worries me because it sounds costly to performance. If, say, a hundred people using the app at the same time, all request to see their favourited recipes, won't that cause massive performance issues in the server for my app?

What should I do? Should I be worried, and find another way to store a user's favourited recipes? How is this usually done in production code? And what may be a better approach? It's my first time creating an app with MongoDB, and I really want this app to be well optimized.

MountainSlayer
  • 291
  • 1
  • 5
  • 14
  • Related [Mongoose populate vs object nesting](https://stackoverflow.com/questions/24096546/mongoose-populate-vs-object-nesting/24096822#24096822) – Neil Lunn Jun 18 '17 at 00:44
  • Also Related [Embedded document vs reference in mongoose design model?](https://stackoverflow.com/questions/21302279/embedded-document-vs-reference-in-mongoose-design-model) – Neil Lunn Jun 18 '17 at 00:45
  • As these generally conclude, it's really up to your application. The general summary being that if actually "small enough" and your more frequent operations ( largely reads ) need to include the embedded information then embed it. The question is largely subjective, but "recipes" is not really going to be hundreds of items long now is it? In general cases we "embed" for smaller activity, and then branch out to "referenced" where there is a "huge" amount of activity. – Neil Lunn Jun 18 '17 at 00:49

0 Answers0