Assignment - 2
Assignment - 2
Section-C
17. What are the data types in MongoDB? 4
18. What are the CRUD operations of MongoDB? Explain with suitable example. 4
19. Suppose a client needs a database design for his blog/website. Website has the following requirements: 4
Every post has the unique title, description and url.
Every post can have one or more tags.
Every post has the name of its publisher and total number of likes.
Every post has comments given by users along with their name, message, data-time and likes.
On each post, there can be zero or more comments.
Create a data model schema design for the website in MongoDB.
20. Create a MovieMaker Database with a collection called “Movies “containing documents with some or all of 4
the following fields: titles, directors, years, actors. Perform the following operations on the database (either in
{
"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"
}
a. Write a MongoDB query to display all the documents in the collection restaurants.
b. Write a MongoDB query to display the fields restaurant_id, name, borough and cuisine for all the
documents in the collection restaurant.
c. 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.
d. Write a MongoDB query to display the fields restaurant_id, name, borough and zip code, but ex-
clude the field _id for all the documents in the collection restaurant.
e. Write a MongoDB query to display all the restaurant which is in the borough Bronx.
f. Write a MongoDB query to display the first 5 restaurant which is in the borough Bronx.
g. Write a MongoDB query to display the next 5 restaurants after skipping first 5 which are in the bor-
ough Bronx.
h. Write a MongoDB query to find the restaurants who achieved a score more than 90.
i. Write a MongoDB query to find the restaurants that achieved a score, more than 80 but less than
100.
j. Write a MongoDB query to find the restaurants which locate in latitude value less than -5.754168.
k. 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.
l. 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.
22. Create a database with the name ‘Movie’. A ‘Film’ is a collection of documents with the following fields: (a)
6
Film Id (b) Title of the film (c) Year of release (d) Genre / Category (like adventure, action, sci-fi, romantic
etc.) A film can belong to more than one genre. (e) Actors (First name and Last name). A film can have more
than one actor. (f) Director (First name and Last name). A film can have more than one director. (g) Release
details. (It consists of places of release, dates of release and rating of the film.). An ‘Actor’ is a collection of
documents with the following fields: (a) Actor Id (b) First name (c) Last Name (d) Address (Street, City,
State, Country, Pin-code) (e) Contact Details (Email Id and Phone No) (f) Age of an actor.
Queries:
1. Insert at least 10 documents in the collection Film –
a. Insert at least one document with film belonging to two genres.
b. Insert at least one document with film that is released at more than one place and on two different dates.
c. Insert at least three documents with the films released in the same year.
d. Insert at least two documents with the films directed by one director.
e. Insert at least two documents with films those are acted by a pair ‘Madhuri Dixit’ and ‘Shahrukh Khan’.
2. Insert at least 10 documents in the collection Actor.
Make sure, you are inserting the names of actors who have acted in films, given in the ‘Film’ collection.
3. Display all the documents inserted in both the collections.
4. Add a value to the rating of the film whose title starts with ‘T’.
5. Add an actor named "Ram" in the ‘Actor’ collection. Also add the details of the film in ‘Film’ collection in
which this actor has acted in.
6. Delete the film “XYZ".
7. Delete an actor named "Jitendra Kumar ".
8. Delete all actors from an ‘Actor’ collection who have age greater than “55”.
9. Update the actor’s address where Actor Id is 101.
10. Update the genre of the film directed by “David Dhawan”.
6
23.
a. Find all the books with a number of pages greater than 250.
b. Find all the books authored by Mario Rossi.
c. Find all the books with a price less than 20 € for Italy (IT).
d. Increase the review score of 0.2 points for all the books with the tag “database”.
e. Insert the tag “NoSQL” for all the books with tag “mongodb”.
f. Insert the publisher for all the documents authored by Mario Rossi with the default value {‘name’:
‘Polito’, city:’Turin’}.
Design a MongoDB database to manage the parcel delivery according to the following requirements: 6
24. Customers of the parcel delivery service are citizens identified by their social security number. They
can be senders or recipients of delivered parcels. They are characterized by their name, surname,
email address, a telephone number, and by different addresses, one for each type (e.g., one billing
address, one home address, one work address, etc.). Each address consists of street name, street
number, postal code, city, province, and country.
Parcels are characterized by a unique barcode and their physical dimensions (specifically: width,
height, depth, and weight). All widths, heights, and depths are always expressed in meters. All
weights are always expressed in kilograms.
The recipient and the sender information required to deliver each parcel must be always available
when accessing the data of a parcel. Recipient and sender information required to deliver a parcel
consists of full name, street name, street number, postal code, city, province, and country.
The parcel warehouse is divided into different areas. Each area is identified by a unique code, e.g.,
'area_51' and consists of different lines. Each line is identified by unique code, e.g., 'line_12', and
hosts several racks. Each rack is identified by unique code, e.g., 'rack_33', and is made up of shelves.
Each parcel is placed on a specific shelf of the warehouse, identified by a unique code, e.g.,
'shelf_99'. The database is required to track the location of each parcel within the warehouse.
Given a parcel, the database must be designed to efficiently provide its full location, from the shelf,
up to the area, through rack and line.
Given a customer, the database must be designed to efficiently provide all her/his parcels as a
sender, and all his/her parcels as a recipient.
Indicate for each collection in the database the document structure and the strategies used for modeling.