2a. Develop a MongoDB query to select certain fields and ignore some fields of the documents from any collection.
//To create the new database as well as switch the database if not existing
use Movies
//To create the collection inside the database
db.createCollection("MovieDetails")
//To insert the multiple values or documents
db.MovieDetails.insertMany([
{ _id: 1, title: "Inception", director: "Christopher Nolan", genre: "Science Fiction", year: 2010, ratings: { imdb: 8.8, rottenTomatoes: 87 } },
{ _id: 2, title: "The Matrix", director: "Wachowskis", genre: "Science Fiction", year: 1999, ratings: { imdb: 8.7, rottenTomatoes: 87 } },
{ _id: 3, title: "The Godfather", director: "Francis Ford Coppola", genre: "Crime", year: 1972, ratings: { imdb: 9.2, rottenTomatoes: 97 } }
]);
Using projection to perform the query:
1. Including Specific Fields:
//To retrieve specific include field values or document
db.MovieDetails.find({}, { title: 1, director: 1 })
OUTPUT:
[
{ _id: 1, title: 'Inception', director: 'Christopher Nolan' },
{ _id: 2, title: 'The Matrix', director: 'Wachowskis' },
{ _id: 3, title: 'The Godfather', director: 'Francis Ford Coppola' }
]
2. Excluding Specific Fields:
//To retrieve specific exclude field values or document
db.MovieDetails.find({}, { ratings: 0 })
OUTPUT:
[
{
_id: 1,
title: 'Inception',
director: 'Christopher Nolan',
genre: 'Science Fiction',
year: 2010
},
{
_id: 2,
title: 'The Matrix',
director: 'Wachowskis',
genre: 'Science Fiction',
year: 1999
},
{
_id: 3,
title: 'The Godfather',
director: 'Francis Ford Coppola',
genre: 'Crime',
year: 1972
}
]
3. Combining Filter and Projection:
// Combine query filter with a projection...
db.MovieDetails.find({ director: "Christopher Nolan" }, { title: 1, year: 1, _id: 0 })
OUTPUT:
[ { title: 'Inception', year: 2010 } ]
2b. Develop a MongoDB query to display the first 5 documents from the results obtained in a. [use of limit and find]
Note: You can use same database and have to add more values or documents in same collection to perform the query as per questions.
db.MovieDetails.insertMany([
{_id: 4, title: "Pulp Fiction", director: "Quentin Tarantino", genre: "Crime", year: 1994, ratings: { imdb: 8.9, rottenTomatoes: 92 } },
{_id: 5, title: "The Shawshank Redemption", director: "Frank Darabont", genre: "Drama", year: 1994, ratings: { imdb: 9.3, rottenTomatoes: 91 } },
{_id: 6, title: "The Dark Knight", director: "Christopher Nolan", genre: "Action", year: 2008, ratings: { imdb: 9.0, rottenTomatoes: 94 } },
{_id: 7, title: "Fight Club", director: "David Fincher", genre: "Drama", year: 1999, ratings: { imdb: 8.8, rottenTomatoes: 79 } }
]);
Query with Projection and Limit:
//Query with Projection and Limit command...
db.MovieDetails.find({}, { title: 1, director: 1, year: 1, _id: 0 }).limit(5)
OUTPUT:
[
{ title: 'Inception', director: 'Christopher Nolan', year: 2010 },
{ title: 'The Matrix', director: 'Wachowskis', year: 1999 },
{ title: 'The Godfather', director: 'Francis Ford Coppola', year: 1972 },
{ title: 'Pulp Fiction', director: 'Quentin Tarantino', year: 1994 },
{ title: 'The Shawshank Redemption', director: 'Frank Darabont', year: 1994 }
]