Movie Search Application
Hello everyone. Here, I will talk about an application that searches a MongoDB database and shows us the properties of the movie we're looking for. Additionally, we'll see the use of RedisCache and acceleration of query operations. You can follow my Github account for source code:
Technologies Used
- HTML
- Node.js
- Express
- Mongoose
- RedisCache
MongoDB
Before going into the details of the application, let's briefly talk about MongoDB. MongoDB, in simple terms, is an open-source NoSQL database application. It has become popular in a short time and is used by a wide audience due to its flexibility and ease of use. We can list its features as follows:
- The primary feature is that it is scalable.
- It stores data as documents. JSON data can be used. Since data is stored as JSON, you won't have any trouble saving data even if the structure of incoming data changes.
- You can store multiple copies of data. This way, you prevent data loss.
- You can create indexes on data. This allows you to access all the data you're looking for quickly and easily.
MongoDB Installation for Windows
- First, start by downloading the MongoDB program suitable for your computer's operating system. If you add the download path to your Windows path system, you can run your commands much more comfortably in the future. You can use this link to download the program.
- Click on the downloaded installation file and complete the installation by following the steps that guide you. Your download path will be: C:\Program Files\MongoDB\Server\3.0.1\bin
- After going to this path, you will see two important files: mongo.exe and mongod.exe
- If you use Command Prompt for MongoDB usage, you'll be comfortable in the next steps. First, add the path you used earlier to environment variables. To do this, right-click on the computer file and go to properties, then click on advanced system settings. Here you will see environment variables.
- After entering environment variables, a window will open. In this window, find the relevant path and paste MongoDB's own path at the end.
- If the process is complete, in the next step you need to determine where your database will be saved. First, open Cmd and run these commands: md \data, md \data\db. By running these commands, you first created a data folder under the C directory, then created a db folder under this data folder.
- Now you can run your database application. Type mongod for the server. If you see a text like "on port 27017" on the screen, you have successfully completed your installation.
Redis Cache
Redis is an in-memory data structure store used as a NoSQL database, for caching, and as a message server. The reason Redis calls itself a data structure store is that it supports more data types than its alternatives. We can list its advantages as follows:
- It is extremely fast because it works synchronously.
- It supports many data types.
- It can save data both on RAM and on disk according to the configuration you set.
- Since it saves to disk, it continues to work with the same data after restart.
- It has an extremely active user base.
- It has many enterprise features such as Sharding, Cluster, Sentinel, Replication.
Running the Project
This project is a web application that uses the Movies.csv file located on the github page and is written with Node.js/Express/MongoDB/RedisCache and we can run it on our local computer. First, we need to import the Movies.csv file to the MongoDB database on your computer.
mongoimport --db databaseName --collection collectionName --type csv --headerline --file csvFileName.csv
GET requests to the root directory return an HTML page containing a text box where the user can enter data and a search button as a response. After a POST request where the user sends a movie name written in this text box to the web server by clicking the search button, all properties of the relevant movie are returned as a response in a table. The movie name that reaches the server with a POST request is first searched in the cache and if found, it is sent back immediately. Movie information not found in the cache is searched in the database and if found, it is both brought to the cache and sent to the user as a response in table format. Hash data structure was used for data stored in the cache.
When running the project, a command screen should be opened in the root directory where the project is located and we need to first do the initialization process to create the environment. After this process, we will get various questions. We can answer them if we want, or we can skip them by pressing enter.
npm init
Then the modules we used while writing the project should be installed in order.
npm install express
npm install redis
npm install body-parser
npm install mongoose
After downloading the modules, we can run our project.
node moviesIndexDemo.js

After entering the port address shown in the command screen into our web browser, our application will appear.

We need to enter a movie name that exists in our database in the search screen on our main page and press the Search button. After the process, all information about the movie we searched for in the database is reflected to us.

If the movie we searched for is not found in Redis Cache, the movie is added to Redis Cache. In other searches, it is used from Redis Cache.

If we leave the box empty, a warning box is shown to us.

When we enter a movie name that is not in the database in the box, a text appears saying that the movie could not be found.


This is how the application's interfaces and functionality work.