A Simple Recommendation System with NodeJS and MongoDB

When we look for AI/ML projects, most likely we find projects based on Python or R, Both are powerful programming languages and comes with features and libraries for machine learning, which makes them the first choice for the most part. But did you know that we can do AI/ML in javascript also?

In this article, we will build a simple Movie Recommender system using Nodejs and MongoDB.

You can check out the demo here.

Note: Neither Nodejs nor MongoDB is an optimal solution for building a full-fledged AI/ML application. This article will give you a basic knowledge of how a recommender system works.

Recommendation System:

To put it simply, A Recommendation system gives you recommendations based on the data it has on you.

Example:

  • Amazon product recommendation.
  • Facebook friends recommendation.
  • Netflix recommendation.
  • YouTube video recommendation.

To implement such a system in our code we need to know how relevant the data are.

Euclidean distance:

Euclidean distance is an algorithm for calculating the distance between two points in a plane or n-dimensional space.

To explain more on that, take two points in scale P and Q on the 2nd and 6th units respectively. If I ask you the distance between these two points, you will reply 4 ( basically 6-2 ). The same logic applies here, but in this case, we consider two points on a plane ( graph ).

Graph 1

In the above picture, the same P and Q points are in a plane and represented in a graph, now what If the question is the distance between these two points.

This is where we use the Euclidean distance formula. which is.

{\displaystyle d(p,q)={\sqrt {(q_{1}-p_{1})^{2}+(q_{2}-p_{2})^{2}}}.}
Euclidean distance formula ( 2 – dimensions ).

Note: the above formula is for calculating distance between two points in a plane ( 2 – dimentional area ). We can also calculate distance betweeen points on higher dimentions, but that is beyond the scope of this article.

If we apply the formula to the above graph, where p1=2,p2=6,q1=10, and q2=8 we get.

√(10-2)2 + (8-6)2 = 12

So 12 is the distance between these two points, we use this value for determining how a value is related to the other. Lower the value the more relevant ( closer ) the two points are.

What does this have to do with recommending stuff? Say take an example we have 3 movies represented in a graph. The graph is plotted as the number of voters vs the average rating given to the movie.

Graph 2 ( Movie data )

Considering the above graph, If a person pics “The Avengers” as his favorite movie and expects recommended movie based on that, Then we can apply the formula in this way, how related are The Avengers, Justice League, and The Avengers, Shawshank redemption. Below will be the results.

Euclidean distance for The Avengers and Justice League is 1.71

Same way, Euclidean distance for The Avengers and Shawshank redemption is 3.21

So clearly, we can see that The Avengers and Justice League are more related in terms of rating and number of votes.

Note: It might not make sense to recommend movies based on number of voters and ratings. To recommend movies, one logical way is the take genre into account also. But for the sake of simplicity we have considered these metrics.

Enough of maths let’s dive into the coding part.

Pre Request:

Before going into coding, below are some things you have to be familiar with.

  • NodeJS and ExpressJS basics.
  • MongoDB basics.
  • NodeJS and npm installed in your system.

These links will get you started.

Recommendation System:

In this section, we will build a simple recommendation system using NodeJS and MongoDB. We will also build a simple interface for listing the recommendation on a Web page. Below is a basic flow of the application.

  • We will have a list of movies to select from, the user clicks on a movie.
  • Below the selected movie, we will display a list of recommended movies in a table.
  • The recommendation will be made on Number of voters and average rating given to that movie.

The data set is obtained from Kaggle ( Do check them out ).

Folder Structure:

Folder structure

Our folder contains two main files index.html for the interface and index.js will run the NodeJS server connected with MongoDB.

Index.js:

This file contains the APIs for getting movie names and get recommendations using MongoDB query.

Index.js ( Part 1 )
  • First we import all the environment variables using dotenv module.
  • Next we do the basic express setup.
  • We use monk for connecting to MongoDB.
  • movie_data is the name of the collection containg our data set.
  • First router ‘/’ will fetch you the index.html file.

Getting all movies:

Index.js ( Part 2 )
  • Here we get the list of movies to display for the user to pick.
  • The data set is huge ( 45467 records ) and it make sense to implement a basic pagination, we do this by limit and skip key, where skip is calculated based on the page given.
  • Then the data is sorted based on _id.

Getting movie recommendadtions:

Index.js ( Part 3 )
  • Here we get the vote_average, vote_count, genres and current _id (selected movie data) from the user.
  • This data set might contain vote_average and vote_count as string meaning “” or “no vote” should be filtered out.
  • Then for more relavance we are matching genre data.
  • We don’t want to get the selected movie in the recommended list, so we are filtering that out.
  • Inside $project we project the needed data to display to the user and $distance contains the The Euclidean distance formula.
  • Then we filter out invalid distance ( NULL ).
  • We sort distance in a way that the minimum values ( closest ) are at the top.
  • We also limit the recommendadtions to 5 ( as discussed earlier, this is a huge data set ).

Running the server:

Index.js ( Part 5 )

We, at last, run the server.

Index.html:

This file contains the interface for our recommender system.

Index.html ( Part 1 )
  • First we have select component containing movies names.
  • Current movie section will contain the selected movie details.
  • The table component will list the recommendastions.
Index.html ( scripting )
  • selectMovie(data) function will update the current movie section and call the getRecommendation api with the required parameters.
  • When the response is obtained, we form a table row and append in the table component.
  • getAllMovies() will fetch all movies with pagination to load in the select component.
Index.html ( scripting )
  • toggleDropDown() function is for toggling the select component.
  • inside onload function, we are listening for the scroll event of the select component for Infinite scrolling.

Note: We can handle large data in the browser using Web Wrokers, check out the tutorial here

Running the application:

To run the application just issue the command npm start in the terminal. You should see the “Server running” text in the command line. Open your browser and navigate to http://localhost:<port> ( port should be configured in the .env file else it will fall back to 8080 ).

Output 1
  • You should see the above screen at the bigining.
  • Select a movie from the drop down and below table will get populated with the recommendations.
Output 2

Conclusion:

This article is intended just to give you a basic introduction to a recommendation system using javascript. In a real-world application, we might need to include a lot of variables to recommend things and the tech stack involved will be very different from the one which is used here. And also Euclidean distance might not suit every scenario, So do your research before building such applications.

Happy coding!!!

If you like my content Buy me a coffee.

GitHub repo: https://github.com/kishork2120/movie-recommender.

For latest updates on tech, do follow us on Instagram.

18 thoughts on “A Simple Recommendation System with NodeJS and MongoDB

  1. One thing I have actually noticed is that there are plenty of fallacies regarding the banks intentions any time talking about foreclosures. One fantasy in particular is the fact the bank wishes to have your house. The financial institution wants your cash, not your home. They want the money they loaned you together with interest. Keeping away from the bank will undoubtedly draw a foreclosed conclusion. Thanks for your post.

  2. It is really a nice and useful piece of information. I抦 glad that you shared this helpful information with us. Please keep us informed like this. Thanks for sharing.

  3. Thank you for some other magnificent post. The place else may just anybody get that kind of information in such a perfect means of writing? I have a presentation next week, and I am at the look for such information.

  4. Hey there! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading through your articles. Can you suggest any other blogs/websites/forums that cover the same subjects? Thanks for your time!

  5. Hey There. I discovered your blog the usage of msn. That is a very well written article. I will make sure to bookmark it and return to read more of your helpful info. Thank you for the post. I抣l certainly return.

  6. Hey There. I discovered your blog the usage of msn.
    That is an extremely smartly written article. I will make sure to bookmark it and
    return to read extra of your helpful info. Thank you
    for the post. I will certainly return.

  7. Hey I know this is off topic but I was wondering
    if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time
    and was hoping maybe you would have some experience with something like this.

    Please let me know if you run into anything. I truly enjoy reading your
    blog and I look forward to your new updates.

  8. I would like to thnkx for the efforts you have put in writing this website. I’m hoping the same high-grade website post from you in the upcoming as well. In fact your creative writing skills has encouraged me to get my own site now. Actually the blogging is spreading its wings fast. Your write up is a great example of it.

  9. Pingback: A simple introduction to data generation with Falso. - Tech Imperialist
  10. A compelling discussion on AI’s integration into marketing tactics. Discover further insights in Profitable Bots’ comprehensive course.

  11. Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.

Leave a Reply to Aretha Cancel reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Translate »