w3resource

MongoDB Exercises, Practice, Solution

What is MongoDB?

MongoDB is a free and open-source cross-platform document-oriented database. Classified as a NoSQL database, MongoDB avoids the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas, making the integration of data in certain types of applications easier and faster.

The best way we learn anything is by practice and exercise questions. We have started this section for those (beginner to intermediate) who are familiar with NoSQL and MongoDB. Hope, these exercises help you to improve your MongoDB query skills. Currently, following exercises are available based on collection :

  • restaurants collection [ 87 Exercises]
  • listingAndReviews collection [ 50 Exercises]
  • Movies collection [ 28 Exercises]
  • MongoDB Query Exercises and Solution [ 87 Exercises]

    Structure of 'restaurants' collection:

    {
      "address": {
         "building": "1007",
         "coord": [ -73.856077, 40.848447 ],
         "street": "Morris Park Ave",
         "zipcode": "10462"
      },
      "borough": "Bronx",
      "cuisine": "Bakery",
      "grades": [
         { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
         { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
         { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
         { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
         { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
      ],
      "name": "Morris Park Bake Shop",
      "restaurant_id": "30075445"
    }
    

    You may download the compressed file and uncompress it to find the collection used in our exercises. The collection comprises of 3772 documents.

    1. Write a MongoDB query to display all the documents in the collection restaurants.
    Click me to see the solution

    2. Write a MongoDB query to display the fields restaurant_id, name, borough and cuisine for all the documents in the collection restaurant.
    Click me to see the solution

    3. Write a MongoDB query to display the fields restaurant_id, name, borough and cuisine, but exclude the field _id for all the documents in the collection restaurant.
    Click me to see the solution

    4. Write a MongoDB query to display the fields restaurant_id, name, borough and zip code, but exclude the field _id for all the documents in the collection restaurant.
    Click me to see the solution

    5. Write a MongoDB query to display all the restaurant which is in the borough Bronx.
    Click me to see the solution

    6. Write a MongoDB query to display the first 5 restaurant which is in the borough Bronx.
    Click me to see the solution

    7.Write a MongoDB query to display the next 5 restaurants after skipping first 5 which are in the borough Bronx.
    Click me to see the solution

    8. Write a MongoDB query to find the restaurants who achieved a score more than 90.
    Click me to see the solution

    9. Write a MongoDB query to find the restaurants that achieved a score, more than 80 but less than 100.
    Click me to see the solution

    10. Write a MongoDB query to find the restaurants which locate in latitude value less than -95.754168.
    Click me to see the solution

    11. Write a MongoDB query to find the restaurants that do not prepare any cuisine of 'American' and their grade score more than 70 and latitude less than -65.754168.
    Click me to see the solution

    12. Write a MongoDB query to find the restaurants which do not prepare any cuisine of 'American' and achieved a score more than 70 and located in the longitude less than -65.754168.
    Note : Do this query without using $and operator.
    Click me to see the solution

    13. Write a MongoDB query to find the restaurants which do not prepare any cuisine of 'American' and achieved a grade point 'A' not belongs to the borough Brooklyn. The document must be displayed according to the cuisine in descending order.
    Click me to see the solution

    14. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'Wil' as first three letters for its name.
    Click me to see the solution

    15. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'ces' as last three letters for its name.
    Click me to see the solution

    16. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'Reg' as three letters somewhere in its name.
    Click me to see the solution

    17. Write a MongoDB query to find the restaurants which belong to the borough Bronx and prepared either American or Chinese dish.
    Click me to see the solution

    18. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which belong to the borough Staten Island or Queens or Bronxor Brooklyn.
    Click me to see the solution

    19. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which are not belonging to the borough Staten Island or Queens or Bronxor Brooklyn.
    Click me to see the solution

    20. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which achieved a score which is not more than 10.
    Click me to see the solution

    21. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which prepared dish except 'American' and 'Chinees' or restaurant's name begins with letter 'Wil'.
    Click me to see the solution

    22. Write a MongoDB query to find the restaurant Id, name, and grades for those restaurants which achieved a grade of "A" and scored 11 on an ISODate "2014-08-11T00:00:00Z" among many of survey dates..
    Click me to see the solution

    23. Write a MongoDB query to find the restaurant Id, name and grades for those restaurants where the 2nd element of grades array contains a grade of "A" and score 9 on an ISODate "2014-08-11T00:00:00Z".
    Click me to see the solution

    24. Write a MongoDB query to find the restaurant Id, name, address and geographical location for those restaurants where 2nd element of coord array contains a value which is more than 42 and upto 52..
    Click me to see the solution

    25. Write a MongoDB query to arrange the name of the restaurants in ascending order along with all the columns.
    Click me to see the solution

    26. Write a MongoDB query to arrange the name of the restaurants in descending along with all the columns.
    Click me to see the solution

    27. Write a MongoDB query to arranged the name of the cuisine in ascending order and for that same cuisine borough should be in descending order.
    Click me to see the solution

    28. Write a MongoDB query to know whether all the addresses contains the street or not.
    Click me to see the solution

    29. Write a MongoDB query which will select all documents in the restaurants collection where the coord field value is Double.
    Click me to see the solution

    30. Write a MongoDB query which will select the restaurant Id, name and grades for those restaurants which returns 0 as a remainder after dividing the score by 7.
    Click me to see the solution

    31. Write a MongoDB query to find the restaurant name, borough, longitude and attitude and cuisine for those restaurants which contains 'mon' as three letters somewhere in its name.
    Click me to see the solution

    32. Write a MongoDB query to find the restaurant name, borough, longitude and latitude and cuisine for those restaurants which contain 'Mad' as first three letters of its name.
    Click me to see the solution

    33. Write a MongoDB query to find the restaurants that have at least one grade with a score of less than 5.
    Click me to see the solution

    34. Write a MongoDB query to find the restaurants that have at least one grade with a score of less than 5 and that are located in the borough of Manhattan.
    Click me to see the solution

    35. Write a MongoDB query to find the restaurants that have at least one grade with a score of less than 5 and that are located in the borough of Manhattan or Brooklyn.
    Click me to see the solution

    36. Write a MongoDB query to find the restaurants that have at least one grade with a score of less than 5 and that are located in the borough of Manhattan or Brooklyn, and their cuisine is not American.
    Click me to see the solution

    37. Write a MongoDB query to find the restaurants that have at least one grade with a score of less than 5 and that are located in the borough of Manhattan or Brooklyn, and their cuisine is not American or Chinese.
    Click me to see the solution

    38. Write a MongoDB query to find the restaurants that have a grade with a score of 2 and a grade with a score of 6.
    Click me to see the solution

    39. Write a MongoDB query to find the restaurants that have a grade with a score of 2 and a grade with a score of 6 and are located in the borough of Manhattan.
    Click me to see the solution

    40. Write a MongoDB query to find the restaurants that have a grade with a score of 2 and a grade with a score of 6 and are located in the borough of Manhattan or Brooklyn.
    Click me to see the solution

    41. Write a MongoDB query to find the restaurants that have a grade with a score of 2 and a grade with a score of 6 and are located in the borough of Manhattan or Brooklyn, and their cuisine is not American.
    Click me to see the solution

    42. Write a MongoDB query to find the restaurants that have a grade with a score of 2 and a grade with a score of 6 and are located in the borough of Manhattan or Brooklyn, and their cuisine is not American or Chinese.
    Click me to see the solution

    43. Write a MongoDB query to find the restaurants that have a grade with a score of 2 or a grade with a score of 6.
    Click me to see the solution

    44. Write a MongoDB query to find the restaurants that have a grade with a score of 2 or a grade with a score of 6 and are located in the borough of Manhattan.
    Click me to see the solution

    45. Write a MongoDB query to find the restaurants that have a grade with a score of 2 or a grade with a score of 6 and are located in the borough of Manhattan or Brooklyn.
    Click me to see the solution

    46. Write a MongoDB query to find the restaurants that have a grade with a score of 2 or a grade with a score of 6 and are located in the borough of Manhattan or Brooklyn, and their cuisine is not American.
    Click me to see the solution

    47. Write a MongoDB query to find the restaurants that have a grade with a score of 2 or a grade with a score of 6 and are located in the borough of Manhattan or Brooklyn, and their cuisine is not American or Chinese.
    Click me to see the solution

    48. Write a MongoDB query to find the restaurants that have all grades with a score greater than 5.
    Click me to see the solution

    49. Write a MongoDB query to find the restaurants that have all grades with a score greater than 5 and are located in the borough of Manhattan.
    Click me to see the solution

    50. Write a MongoDB query to find the restaurants that have all grades with a score greater than 5 and are located in the borough of Manhattan or Brooklyn.
    Click me to see the solution

    51. Write a MongoDB query to find the average score for each restaurant.
    Click me to see the solution

    52. Write a MongoDB query to find the highest score for each restaurant.
    Click me to see the solution

    53. Write a MongoDB query to find the lowest score for each restaurant.
    Click me to see the solution

    54. Write a MongoDB query to find the count of restaurants in each borough.
    Click me to see the solution

    55. Write a MongoDB query to find the count of restaurants for each cuisine.
    Click me to see the solution

    56. Write a MongoDB query to find the count of restaurants for each cuisine and borough.
    Click me to see the solution

    57. Write a MongoDB query to find the count of restaurants that received a grade of 'A' for each cuisine.
    Click me to see the solution

    58. Write a MongoDB query to find the count of restaurants that received a grade of 'A' for each borough.
    Click me to see the solution

    59. Write a MongoDB query to find the count of restaurants that received a grade of 'A' for each cuisine and borough.
    Click me to see the solution

    60. Write a MongoDB query to find the number of restaurants that have been graded in each month of the year.
    Click me to see the solution

    61. Write a MongoDB query to find the average score for each cuisine.
    Click me to see the solution

    62. Write a MongoDB query to find the highest score for each cuisine.
    Click me to see the solution

    63. Write a MongoDB query to find the lowest score for each cuisine.
    Click me to see the solution

    64. Write a MongoDB query to find the average score for each borough.
    Click me to see the solution

    65. Write a MongoDB query to find the highest score for each borough.
    Click me to see the solution

    66. Write a MongoDB query to find the lowest score for each borough.
    Click me to see the solution

    67. Write a MongoDB query to find the name and address of the restaurants that received a grade of 'A' on a specific date.
    Click me to see the solution

    68. Write a MongoDB query to find the name and address of the restaurants that received a grade of 'B' or 'C' on a specific date.
    Click me to see the solution

    69. Write a MongoDB query to find the name and address of the restaurants that have at least one 'A' grade and one 'B' grade.
    Click me to see the solution

    70. Write a MongoDB query to find the name and address of the restaurants that have at least one 'A' grade and no 'B' grades.
    Click me to see the solution

    71. Write a MongoDB query to find the name ,address and grades of the restaurants that have at least one 'A' grade and no 'C' grades.
    Click me to see the solution

    72. Write a MongoDB query to find the name, address, and grades of the restaurants that have at least one 'A' grade, no 'B' grades, and no 'C' grades.
    Click me to see the solution

    73. Write a MongoDB query to find the name and address of the restaurants that have the word 'coffee' in their name.
    Click me to see the solution

    74. Write a MongoDB query to find the name and address of the restaurants that have a zipcode that starts with '10'.
    Click me to see the solution

    75. Write a MongoDB query to find the name and address of the restaurants that have a cuisine that starts with the letter 'B'.
    Click me to see the solution

    76. Write a MongoDB query to find the name, address, and cuisine of the restaurants that have a cuisine that ends with the letter 'y'.
    Click me to see the solution

    77. Write a MongoDB query to find the name, address, and cuisine of the restaurants that have a cuisine that contains the word 'Pizza'.
    Click me to see the solution

    78. Write a MongoDB query to find the restaurants achieved highest average score.
    Click me to see the solution

    79. Write a MongoDB query to find all the restaurants with the highest number of "A" grades.
    Click me to see the solution

    80. Write a MongoDB query to find the cuisine type that is most likely to receive a "C" grade.
    Click me to see the solution

    81. Write a MongoDB query to find the restaurant that has the highest average score for thecuisine "Turkish".
    Click me to see the solution

    82. Write a MongoDB query to find the restaurants that achieved the highest total score.
    Click me to see the solution

    83. Write a MongoDB query to find all the Chinese restaurants in Brooklyn.
    Click me to see the solution

    84. Write a MongoDB query to find the restaurant with the most recent grade date.
    Click me to see the solution

    85. Write a MongoDB query to find the top 5 restaurants with the highest average score for each cuisine type, along with their average scores.
    Click me to see the solution

    86. Write a MongoDB query to find the top 5 restaurants in each borough with the highest number of "A" grades.
    Click me to see the solution

    87. Write a MongoDB query to find the borough with the highest number of restaurants that have a grade of "A" and a score greater than or equal to 90.
    Click me to see the solution

    Click here to get Sample Restaurants Dataset from MongoDB.

    More to Come !

    

    Become a Patron!

    Follow us on Facebook and Twitter for latest update.

    It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

    https://w3resource.com/mongodb-exercises/