SQL代写,数据库代写,数据库作业代写,SQL作业代写
Directions: Go through and answer the questions below. Submit a document that includes screenshots of query and results along with a brief explanation for each question.
MongoDB
Go to the database practice environment: http://www.pdbmbook.com/practice (链接到外部网站。)
to complete the following exercise using the MongoDB and the restaurant dataset.
Before you do the exercise, you may want to complete watching the practice environment tutorial video (链接到外部网站。)
-- MangoDB section, which starts around 6:11 in the video.
Make sure your screenshots are readable (both query and results) and be sure to include explanations for your queries when asked. You are recommended to call the .pretty() method at the end of each query to print results in a nicely formatted form; if the returned results are long, only including the first page of results in your screenshot should be fine.
- Write a MongoDB query to display the fields, restaurant_id, name, location, and type_of_food for all documents in the collection, restaurant. (Note: The practice environment only displays the first 20, and this is fine). Describe the syntax of the query for your explanation.
- Write a MongoDB query to display all restaurants that are in the location, London. Describe the syntax of this query.
- Write a query to find all the Chinese restaurants in the location, London. Display only the restaurant_id, name, location, and type_of_food. Describe how this query is different from your previous.
- Using the db.collection.insert command, insert the following restaurant into the existing database of restaurants:
name: Little Szechuan
location: State College
address: 228 W College Ave
type_of_food: Chinese
rating: 5
Show a screenshot of your command and the result.
- Query the database to pull all Chinese restaurants to see if you can display yours. If this doesn't work, limit/filter the query further in order to display the Chinese restaurant you added along with some others (e.g., all Chinese restaurants with rating of 5). Show a screenshot.
- What is the difference between a document and a collection in MongoDB?
- a) Write a query to return how many restaurants are in the database. b) Write a query to return the number of restaurants in Glasgow (not a list of the restaurants).
MongoDB is not a relational database and cannot perform join operations. However, a concept called aggregation pipelines can be used to essentially perform this task between two collections (aggregation pipelines can also be used to perform calculations or complex queries). Let's try to use an aggregation function to join information from the people collection and the restaurants collection in the practice environment.
8. Explore the people collection by looking at the entries using a db.collection.find query. What does it look like this collection contains? Show a screenshot to document your answer.
9. See if you can "join" the people collection (COLLECTION1) and the restaurant collection (COLLECTON2) by matching on the restaurant ID. To do this, you will use an aggregation pipeline which has the syntax shown below. The fields are the join fields or the fields you want to match on between the collections. Remember the names of the fields are strings and will have to be in single quotes. An example can be found in an online tutorial (链接到外部网站。). You can also read about the syntax of the $lookup operator in the reference manual (链接到外部网站。).
db.COLLECTION1.aggregate([
{$lookup:
{
from: 'COLLECTION2',
localField: FIELD_COLLECTION1,
foreignField: FIELD_COLLECTION2,
as: NEW_NAME
}
}])
Where NEW_NAME is a string of any new name you want to call the field in the new combined collection (the field that will be the one that is matched between collection 1 and collection 2. You can name it anything you want.)
