2 A
2 A
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
db.createCollection("MovieDetails")
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 } }
]);
OUTPUT:
db.MovieDetails.find({}, { ratings: 0 })
OUTPUT:
[
{
_id: 1,
title: 'Inception',
year: 2010
},
_id: 2,
director: 'Wachowskis',
year: 1999
},
_id: 3,
genre: 'Crime',
year: 1972
OUTPUT:
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 } }
]);
OUTPUT: