Getting Started With MongoDB Query!

MongoDB is a document database designed for ease of development and scaling. A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents. For ease and better understanding of the queries in mongoDB, i’ll be comparing SQL database structure with the database structure of mongoDB(NoSQL).


KEY POINTS:-

  1. SQL database consists of TABLES and NoSQL database consists of collections.

Which are simply different ways of representing and storing datas.

  1. Please go through the MongoDB documentation( https://docs.mongodb.com/manual/reference/operator/query/ ) on queries before going through the project to understand the basics .


Before writing queries let’s connect mongoDB atlas with our system :-

1) Download mongoDB Atlas from https://www.mongodb.com/try .

2) After installing mongoDB Atlas, open it and connect it with the required PORT or connect it with the default PORT(27017) by clicking on connect.




3) Create a new database and create a new collection inside of that database by selecting the CREATE DATABASE and entering name of database and collection.



4) For this project import an existing dataset by clicking the IMPORT option under collection or you can add data manually.


5) After completing all the steps you are ready to start writing queries in the mongoDB atlas itself and later we’ll show how to write queries on any other terminal such as CMD prompt.


6) Here I have downloaded the dataset from, http://grouplens.org/datasets/movielens/ . Use the small dataset (ml-100) from the category MovieLens Latest Datasets section



Getting Started with Queries :


1. Generate a list of movies that have “Adventure” as a genre.


Hint: Simple queries like the above one could be written directly on the options bar given above the collection, then click on the find option to run the required query.In the above query we are finding the genres that consist of the adventure word in it.


2. Generate the movies that have been categorized into more than 2 genres.


Hint: For solving this query we checking if the genres field of each document consist of “|” in it. If it consist of “|” that shows it has more than one category under genres.


3. Generate a list of movies that have the “Children” genre and a user rating > 3.


Hint: In the above query we are using two collections, movies and ratings. So before writing any query we have to join the two tables and then write the required query. Note that for writing multiple queries aggregate keyword is being used (i.e. db.movies.aggregate([ <queries>])), and in mongoDB Atlas you can directly do it using the aggregation tab mentioned on top.


And for different queries under aggregation use the boxes given on the left and the result will be displayed on the right. You can select the queries from the dropdown as shown below


4. Generate all users whose average rating across the movies they have rated is greater than 3


5. What are the genres that appear in movies that are rated greater than 4?


6. Which Star Wars movie has the highest average user rating and what is the average rating?


7. A. What is the average rating of all movies with a date of 2008 in the title?


7.B. What is the average rating of all movies with Star Wars in the title?

Try to solve this on your own if you face any issue go through the solution given below.



Run on CMD

NOTE: TO RUN THE FOLLOWING QUERIES ON COMMAND LINE TERMINAL FOLLOW THE GIVEN STEPS,

1) Open CMD prompt.

2) Change the current directory to the directory where you have installed mongoDB using (cd) command.

E.g: cd C:\Program Files\MongoDB\Server\4.4\bin

After changing the directory run the mongoDB server by typing the keyword (mongo) in terminal and you’ll get the given response



Contact Us to Get solution of these queries or other MongoDB Project Related Help at affordable price then you can contact us at: contact@codersarts.com

5 views0 comments

Recent Posts

See All