Skip to content

Commit dc535e5

Browse files
committed
create assignment
1 parent ca5b41a commit dc535e5

File tree

3 files changed

+4050
-2
lines changed

3 files changed

+4050
-2
lines changed

QUERIES.md

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# MongoDB Queries Practice
2+
3+
Write a MongoDB query to:
4+
5+
1. Display all the documents in the restaurants collection.
6+
```
7+
Paste your solution here
8+
```
9+
10+
2. Display the fields restaurant_id, name, borough and cuisine for all the documents in the collection.
11+
```
12+
Paste your solution here
13+
```
14+
15+
3. Display the fields restaurant_id, name, borough and cuisine, but exclude the field _id for all the documents in the collection.
16+
```
17+
Paste your solution here
18+
```
19+
20+
4. Display the fields restaurant_id, name, borough and zip code, but exclude the field _id for all the documents in the collection.
21+
```
22+
Paste your solution here
23+
```
24+
25+
5. Display all the restaurants which are in the borough Bronx.
26+
```
27+
Paste your solution here
28+
```
29+
30+
6. Display the first 5 restaurants which are in the borough Bronx.
31+
```
32+
Paste your solution here
33+
```
34+
35+
7. Display the next 5 restaurants after skipping first 5 which are in the borough Bronx.
36+
```
37+
Paste your solution here
38+
```
39+
40+
8. Find the restaurants who achieved a score more than 90.
41+
```
42+
Paste your solution here
43+
```
44+
45+
9. Find the restaurants that achieved a score, more than 80 but less than 100.
46+
```
47+
Paste your solution here
48+
```
49+
50+
10. Find the restaurants which are located in latitude value less than -95.754168.
51+
```
52+
Paste your solution here
53+
```
54+
55+
11. Find the restaurants that do not prepare any cuisine of 'American' and their grade score is more than 70 and latitude less than -65.754168.
56+
```
57+
Paste your solution here
58+
```
59+
60+
12. Find the restaurants which do not prepare any cuisine of 'American' and achieved a score more than 70 and are located in the longitude less than -65.754168.
61+
62+
Note : Do this query without using $and operator.
63+
```
64+
Paste your solution here
65+
```
66+
67+
13. Find the restaurants which do not prepare any cuisine of 'American' and achieved a grade point 'A' and does not belong to the borough Brooklyn. The document must be displayed according to the cuisine in descending order.
68+
```
69+
Paste your solution here
70+
```
71+
72+
14. Find the restaurant_id, name, borough and cuisine for those restaurants which contain 'Wil' as first three letters in its name.
73+
```
74+
Paste your solution here
75+
```
76+
77+
15. Find the restaurant_id, name, borough and cuisine for those restaurants which contain 'ces' as last three letters in its name.
78+
```
79+
Paste your solution here
80+
```
81+
82+
16. Find the restaurant_id, name, borough and cuisine for those restaurants which contain 'Reg' as three letters somewhere in its name.
83+
```
84+
Paste your solution here
85+
```
86+
87+
17. Find the restaurants which belong to the borough Bronx and prepare either American or Chinese cuisine.
88+
```
89+
Paste your solution here
90+
```
91+
92+
18. Find the restaurant_id, name, borough and cuisine for those restaurants which belong to the boroughs Staten Island or Queens or Bronx or Brooklyn.
93+
```
94+
Paste your solution here
95+
```
96+
97+
19. Find the restaurant_id, name, borough and cuisine for those restaurants which don't belong to the boroughs Staten Island or Queens or Bronx or Brooklyn.
98+
```
99+
Paste your solution here
100+
```
101+
102+
20. Find the restaurant_id, name, borough and cuisine for those restaurants which achieved a score that is not more than 10.
103+
```
104+
Paste your solution here
105+
```
106+
107+
21. Find the restaurant_id, name, borough and cuisine for those restaurants which either prepare dishes except 'American' and 'Chinese' or has a name which begins with the letters 'Wil'.
108+
```
109+
Paste your solution here
110+
```
111+
112+
22. Find the restaurant_id, name, and grades for those restaurants which achieved an A grade and scored 11 on the date "2014-08-11T00:00:00Z" among many of survey dates.
113+
```
114+
Paste your solution here
115+
```
116+
117+
23. 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".
118+
```
119+
Paste your solution here
120+
```
121+
122+
24. 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.
123+
```
124+
Paste your solution here
125+
```
126+
127+
25. Arrange the name of the restaurants in ascending order along with all the columns.
128+
```
129+
Paste your solution here
130+
```
131+
132+
26. Arrange the name of the restaurants in descending along with all the columns.
133+
```
134+
Paste your solution here
135+
```
136+
137+
27. Arrange the restaurants in ascending order of cuisines and descending order of boroughs.
138+
```
139+
Paste your solution here
140+
```
141+
142+
28. Check whether all the resturant addresses contain the street or not.
143+
```
144+
Paste your solution here
145+
```
146+
147+
29. Display all documents in the restaurants collection where the coord field value is a Double.
148+
```
149+
Paste your solution here
150+
```
151+
152+
30. Display the restaurant_id, name and grades for those restaurants which have a grade divisible by 7.
153+
```
154+
Paste your solution here
155+
```
156+
157+
31. Find the restaurant name, borough, longitude and latitude and cuisine for those restaurants which contain 'mon' as three letters somewhere in its name.
158+
```
159+
Paste your solution here
160+
```

README.md

+118-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,118 @@
1-
# curriculum-backend-mongodb-queries-practice
2-
MongoDB Queries Practice
1+
# MongoDB Queries Practice
2+
The objectives of this assignment are:
3+
1. Practicing MongoDB queries on a local database
4+
2. Learning to query MongoDB collections directly before implementing the same in CRUD APIs
5+
6+
## Setup
7+
Unlike the SQL assignment, this time we'll be working with a local database. So we will need to complete the local setup before we begin.
8+
9+
### Install MongoDB
10+
Let's start by installing MongoDB on our local environments. Follow [these instructions on the MongoDB documentation](https://docs.mongodb.com/manual/installation/) to download and install MongoDB as per your OS.
11+
12+
You can also [follow these instructions from the Microsoft documentation](https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-database) if you are using WSL on your machine.
13+
14+
**NOTE** : Make sure to install the *Community Edition of MongoDB* and check that the version being installed is MongoDB 5.0, which is the latest release.
15+
16+
### Verify your installation
17+
After following the installation instructions, run the following command:
18+
```
19+
mongod --version
20+
```
21+
You should see an output similar to this:
22+
```
23+
db version v5.0.3
24+
Build Info: {
25+
"version": "5.0.3",
26+
"gitVersion": "657fea5a61a74d7a79df7aff8e4bcf0bc742b748",
27+
"modules": [],
28+
"allocator": "system",
29+
"environment": {
30+
"distarch": "x86_64",
31+
"target_arch": "x86_64"
32+
}
33+
}
34+
```
35+
This confirms that we have installed MongoDB version 5.0.
36+
37+
### Run MongoDB
38+
For Linux, run the command to start running the MongoDB service:
39+
```
40+
sudo systemctl start mongod
41+
```
42+
Verify that MongoDB has started successfully by running the command:
43+
```
44+
sudo systemctl status mongod
45+
```
46+
47+
For MacOS, run the command to start running the MongoDB service:
48+
```
49+
brew services start [email protected]
50+
```
51+
Verify that MongoDB has started successfully by running the command:
52+
```
53+
brew services list
54+
```
55+
56+
### Prepare local database
57+
To begin using MongoDB, connect the Mongo shell to the running instance simply by running the command: `mongosh`
58+
It should show connection information and open a prompt looking like: `test>`
59+
60+
By default the local MongoDB setup includes a database called `test`. You can verify this now by running the command `db` and it should print the name of the current database which is `test`.
61+
62+
Let's create a new database called `practice`. Run this command: `use practice`. You should get an output `switched to db practice` and the prompt should change to `practice>`. This means we are now ready to use our new database.
63+
64+
If we now run the command `show collections` we can see that there are currently no collections on our database. On this assignment repo, you will find a file called `restaurants.json` which is a collection of restaurant data. Let's load this collection into our database. Open a new terminal window and run this command:
65+
```
66+
mongoimport --db practice --collection restaurants --drop --file /Users/shrreya/Desktop/Projects/ReCoded/curriculum-backend-mongodb-queries-practice/restaurants.json
67+
```
68+
The output should mention:
69+
```
70+
3772 document(s) imported successfully
71+
```
72+
73+
Now if we return to the terminal window running Mongo shell and run `show collections` again we will see that our database now has a collection called `restaurants`.
74+
75+
Let's run a query to look at the first document in the collection:
76+
```
77+
db.restaurants.findOne()
78+
```
79+
80+
If you see all the details of the first restaurant, you have completed the setup successfully!
81+
82+
Looking at this first document, you should be able to understand the document model.
83+
```
84+
{
85+
_id: ObjectId,
86+
address: Object {
87+
building: String,
88+
coord: Array of numbers,
89+
street: String,
90+
zipcode: String
91+
},
92+
borough: String,
93+
cuisine: String,
94+
grades: Array of objects {date: Date, grade: String, score: Number},
95+
name: String,
96+
restaurant_id: String
97+
}
98+
```
99+
100+
If you want to exit the Mongo shell, you can just type the command: `exit`
101+
102+
## Practice Time
103+
Let's perform some queries on our restaurants collection now. In this assignment repo, you will find another markdown file called `QUERIES.md`. In this file, we have listed many practice queries for you. Go through the questions one by one and try to execute the queries on your Mongo shell.
104+
105+
You can keep the MongoDB documentation open on your browser to help you with the queries.
106+
107+
As you finish executing your queries, paste the solutions in the provided space inside markdown code blocks syntax below each query statement.
108+
109+
## Submission
110+
Once you're ready to submit the assignment, follow these steps on your terminal:
111+
1. Stage your changes to the `QUERIES.md` file to be committed: `git add QUERIES.md`
112+
2. Commit your final changes: `git commit -m "solve assignment"`
113+
3. Push your commit to the main branch of your assignment repo: `git push origin main`
114+
115+
After your changes are pushed, return to this assignment on Canvas for the final step of submission.
116+
117+
## Conclusion
118+
Now that we have practiced performing queries on a MongoDB database, we are ready for module 3 where we will learn to build a CRUD API using Express.js and a MongoDB ODM.

0 commit comments

Comments
 (0)