Mangodb games , movies
Games :-
A. Create a collection called ‘games’. We’re going to put some games in it.
Creation -
db.createCollection('games');
B. Add 5 games to the database. Give each document the following properties: name, genre, rating (out
of 100) If you make some mistakes and want to clean it out, use remove()on your collection.
Insertion -
db.games.insertMany([
{
name: "The Legend of Zelda: Breath of the Wild",
genre: "Action-Adventure",
rating: 97,
achievements: {}
},
{
name: "Hollow Knight",
genre: "Metroidvania",
rating: 90,
achievements: {}
},
{
name: "Celeste",
genre: "Platformer",
rating: 94,
achievements: {}
},
{
name: "God of War",
genre: "Action",
rating: 93,
achievements: {}
},
{
name: "Stardew Valley",
genre: "Simulation",
rating: 89,
achievements: {}
}
]);
C. Write a query that returns all the games
db.games.find().pretty();
Output -
{
"_id": ObjectId("..."),
"name": "The Legend of Zelda: Breath of the Wild",
"genre": "Action-Adventure",
"rating": 97,
"achievements": {}
},
{
"_id": ObjectId("..."),
"name": "Hollow Knight",
"genre": "Metroidvania",
"rating": 90,
"achievements": {}
},
{
"_id": ObjectId("..."),
"name": "Celeste",
"genre": "Platformer",
"rating": 94,
"achievements": {}
},
{
"_id": ObjectId("..."),
"name": "God of War",
"genre": "Action",
"rating": 93,
"achievements": {}
},
{
"_id": ObjectId("..."),
"name": "Stardew Valley",
"genre": "Simulation",
"rating": 89,
"achievements": {}
}
D. Write a query to find one of your games by name without using limit(). Use the findOne method. Look
how much nicer it’s formatted!
db.games.findOne({ name: "Hollow Knight" });
Output -
{
"_id": ObjectId("..."),
"name": "Hollow Knight",
"genre": "Metroidvania",
"rating": 90,
"achievements": {}
}
E. Write a query that returns the 3 highest rated games.
db.games.find().sort({ rating: -1 }).limit(3);
Output -
{
"_id": ObjectId("..."),
"name": "The Legend of Zelda: Breath of the Wild",
"genre": "Action-Adventure",
"rating": 97,
"achievements": {}
},
{
"_id": ObjectId("..."),
"name": "Celeste",
"genre": "Platformer",
"rating": 94,
"achievements": {}
},
{
"_id": ObjectId("..."),
"name": "God of War",
"genre": "Action",
"rating": 93,
"achievements": {}
}
F. Update your two favourite games to have two achievements called ‘Game Master’ and ‘Speed
Demon’, each under a single key. Show two ways to do this. Do the first using update()and do the
second using save(). Hint: for save, you might want to query the object and store it in a variable first.
Update -
db.games.update(
{ name: "The Legend of Zelda: Breath of the Wild" },
{ $set: { achievements: { "Game Master": true, "Speed Demon": true } } }
);
db.games.update(
{ name: "God of War" },
{ $set: { achievements: { "Game Master": true, "Speed Demon": true } } }
);
Save - We used replaceOne as alternative command for save()
let zeldaGame = db.games.findOne({ name: "The Legend of Zelda: Breath of the Wild" });
zeldaGame.achievements = { "Game Master": true, "Speed Demon": true };
db.games.replaceOne(zeldaGame);
let godOfWarGame = db.games.findOne({ name: "God of War" });
godOfWarGame.achievements = { "Game Master": true, "Speed Demon": true };
db.games.replaceOne(godOfWarGame);
G. Write a query that returns all the games that have both the ‘Game Maser’ and the ‘Speed Demon’
achievements.
db.games.find({ achievements: { $exists: true, $ne: {} } });
Output -
{
"_id": ObjectId("..."),
"name": "The Legend of Zelda: Breath of the Wild",
"genre": "Action-Adventure",
"rating": 97,
"achievements": { "Game Master": true, "Speed Demon": true }
},
{
"_id": ObjectId("..."),
"name": "God of War",
"genre": "Action",
"rating": 93,
"achievements": { "Game Master": true, "Speed Demon": true }
}
H. Write a query that returns only games that have achievements. Not all of your games should have
achievements, obviously.
db.games.find({ achievements: { $exists: true } });
Output -
{
"_id": ObjectId("..."),
"name": "The Legend of Zelda: Breath of the Wild",
"genre": "Action-Adventure",
"rating": 97,
"achievements": { "Game Master": true, "Speed Demon": true }
},
{
"_id": ObjectId("..."),
"name": "God of War",
"genre": "Action",
"rating": 93,
"achievements": { "Game Master": true, "Speed Demon": true }
}
Movies Collection
A. Write a MongoDB query to display the total number of documents in the collection movies.
Creation -
db.createCollection('movies');
Insertion -
db.movies.insertMany([
{
title: "Inception",
genres: ["Action", "Adventure", "Sci-Fi"],
awards: 4,
cast: ["Leonardo DiCaprio", "Joseph Gordon-Levitt"],
director: "Christopher Nolan"
},
{
title: "The Lord of the Rings: The Return of the King",
genres: ["Action", "Adventure", "Fantasy"],
awards: 11,
cast: ["Elijah Wood", "Ian McKellen"],
director: "Peter Jackson"
},
{
title: "Forrest Gump",
genres: ["Drama", "Romance"],
awards: 6,
cast: ["Tom Hanks", "Robin Wright"],
director: "Robert Zemeckis"
},
{
title: "Baahubali: The Conclusion",
genres: ["Action", "Adventure", "Drama"],
awards: 3,
cast: ["Prabhas", "Rana Daggubati"],
director: "S S Rajamouli"
},
{
title: "Zindagi Na Milegi Dobara",
genres: ["Drama", "Adventure"],
awards: 2,
cast: ["Hrithik Roshan", "Farhan Akhtar"],
director: "Zoya Akhtar"
},
{
title: "Pursuit of Happyness",
genres: ["Drama"],
awards: 1,
cast: ["Will Smith", "Thandie Newton"],
director: "Gabriele Muccino"
},
{
title: "3 Idiots",
genres: ["Comedy", "Drama"],
awards: 3,
cast: ["Aamir Khan", "Kareena Kapoor"],
director: "Rajkumar Hirani"
}
]);
db.movies.countDocuments();
Output -
7
B. Write a MongoDB query to display any 5 documents using pretty format in the collection movies.
db.movies.find().limit(5).pretty();
Output -
{
"_id": ObjectId("..."),
"title": "Inception",
"genres": ["Action", "Adventure", "Sci-Fi"],
"awards": 4,
"cast": ["Leonardo DiCaprio", "Joseph Gordon-Levitt"],
"director": "Christopher Nolan"
},
{
"_id": ObjectId("..."),
"title": "The Lord of the Rings: The Return of the King",
"genres": ["Action", "Adventure", "Fantasy"],
"awards": 11,
"cast": ["Elijah Wood", "Ian McKellen"],
"director": "Peter Jackson"
},
{
"_id": ObjectId("..."),
"title": "Forrest Gump",
"genres": ["Drama", "Romance"],
"awards": 6,
"cast": ["Tom Hanks", "Robin Wright"],
"director": "Robert Zemeckis"
},
{
"_id": ObjectId("..."),
"title": "Baahubali: The Conclusion",
"genres": ["Action", "Adventure", "Drama"],
"awards": 3,
"cast": ["Prabhas", "Rana Daggubati"],
"director": "S S Rajamouli"
},
{
"_id": ObjectId("..."),
"title": "Zindagi Na Milegi Dobara",
"genres": ["Drama", "Adventure"],
"awards": 2,
"cast": ["Hrithik Roshan", "Farhan Akhtar"],
"director": "Zoya Akhtar"
}
C. Write a MongoDB query to display 5 documents sorted by “title” using pretty format in the collection
movies.
db.movies.find().sort({ title: 1 }).limit(5).pretty();
Output -
{
"_id": ObjectId("..."),
"title": "3 Idiots",
"genres": ["Comedy", "Drama"],
"awards": 3,
"cast": ["Aamir Khan", "Kareena Kapoor"],
"director": "Rajkumar Hirani"
},
{
"_id": ObjectId("..."),
"title": "Baahubali: The Conclusion",
"genres": ["Action", "Adventure", "Drama"],
"awards": 3,
"cast": ["Prabhas", "Rana Daggubati"],
"director": "S S Rajamouli"
},
{
"_id": ObjectId("..."),
"title": "Forrest Gump",
"genres": ["Drama", "Romance"],
"awards": 6,
"cast": ["Tom Hanks", "Robin Wright"],
"director": "Robert Zemeckis"
},
{
"_id": ObjectId("..."),
"title": "Inception",
"genres": ["Action", "Adventure", "Sci-Fi"],
"awards": 4,
"cast": ["Leonardo DiCaprio", "Joseph Gordon-Levitt"],
"director": "Christopher Nolan"
},
{
"_id": ObjectId("..."),
"title": "The Lord of the Rings: The Return of the King",
"genres": ["Action", "Adventure", "Fantasy"],
"awards": 11,
"cast": ["Elijah Wood", "Ian McKellen"],
"director": "Peter Jackson"
}
D. Write a MongoDB query to display 5 documents (display only title and awards) sorted by “title” using
pretty format in the collection movies.
db.movies.find({}, { title: 1, awards: 1 }).sort({ title: 1 }).limit(5).pretty();
E. Write a MongoDB query to display 5 documents (display only title and awards) sorted by “title” in descending order
db.movies.find({}, { title: 1, awards: 1 }).sort({ title: -1 }).limit(5).pretty();
F. Write a MongoDB query to display movies (display only title and awards) with most awards (number
of awards in descending order).
db.movies.find({}, { title: 1, awards: 1 }).sort({ awards: -1 }).limit(5).pretty();
Output -
{
"_id": ObjectId("..."),
"title": "The Lord of the Rings: The Return of the King",
"awards": 11
},
{
"_id": ObjectId("..."),
"title": "Forrest Gump",
"awards": 6
},
{
"_id": ObjectId("..."),
"title": "Inception",
"awards": 4
},
{
"_id": ObjectId("..."),
"title": "Baahubali: The Conclusion",
"awards": 3
},
{
"_id": ObjectId("..."),
"title": "3 Idiots",
"awards": 3
G. Write a MongoDB query to display the details of the movie that won most awards
db.movies.find().sort({ awards: -1 }).limit(1).pretty();
Output -
{
"_id": ObjectId("..."),
"title": "The Lord of the Rings: The Return of the King",
"genres": ["Action", "Adventure", "Fantasy"],
"awards": 11,
"cast": ["Elijah Wood", "Ian McKellen"],
"director": "Peter Jackson"
}
H. Write a MongoDB query to display any 5 movies with both the genres: “Adventure” and “Movie” in
collection movies (use $all)
db.movies.find({ genres: { $all: ["Adventure", "Movie"] } }).limit(5).pretty();
Output -
No Output
I. Write a MongoDB query to display any 5 movies with both the condition: genres “Adventure” and cast
“Tom Hanks”.
db.movies.find({ genres: "Adventure", cast: "Tom Hanks" }).limit(5).pretty();
Output -
{
"_id": ObjectId("..."),
"title": "Forrest Gump",
"genres": ["Drama", "Romance"],
"awards": 6,
"cast": ["Tom Hanks", "Robin Wright"],
"director": "Robert Zemeckis"
}
J. Write a MongoDB query to display the total number of documents in the collection movies.
db.movies.countDocuments();
Output -
7
K. Write a MongoDB query to display 5 movie which has director name of S S Rajamouli sorted by “title”
in descending order using pretty format in the collection movies
db.movies.find({ director: "S S Rajamouli" }).sort({ title: -1 }).limit(5).pretty();
Output -
{
"_id": ObjectId("..."),
"title": "Baahubali: The Conclusion",
"genres": ["Action", "Adventure", "Drama"],
"awards": 3,
"cast": ["Prabhas", "Rana Daggubati"],
"director": "S S Rajamouli"
}
L. Write a MongoDB query to display 5 movie which has actor name of Hrithik Roshan sorted by “title” in
descending order using pretty format in the collection movies
db.movies.find({ cast: "Hrithik Roshan" }).sort({ title: -1 }).limit(5).pretty();
Output -
{
"_id": ObjectId("..."),
"title": "Zindagi Na Milegi Dobara",
"genres": ["Drama", "Adventure"],
"awards": 2,
"cast": ["Hrithik Roshan", "Farhan Akhtar"],
"director": "Zoya Akhtar"
}
M. Write a MongoDB query to display movies which has actor name of Kamal Haasan (display only title
db.movies.find({ cast: "Kamal Haasan" }, { title: 1, awards: 1 }).sort({ awards: -1 }).limit(1).pretty();
Output -
No Output
N. Write a MongoDB query to count movies which are in bollywood
db.movies.countDocuments({ genres: "Bollywood" });
Comments
Post a Comment