// Start MongoDB server
mongod &

// In a different terminal to avoid spurious server feedback
// Connect to MongoDB
mongosh

use moviedb                 // Create and use a database

db.createCollection("movies")       // Create a collection

show collections            // Verify the collection was created

db.movies.insertOne({       // Insert a single movie
  "title": "Inception",
  "year": 2010,
  "genres": ["Action", "Sci-Fi", "Thriller"],
  "director": "Christopher Nolan",
  "actors": ["Leonardo DiCaprio", "Ken Watanabe"],
  "rating": 8.8
})

db.movies.insertMany([      // Insert multiple movies
  {
    "title": "The Shawshank Redemption",
    "year": 1994,
    "genres": ["Drama"],
    "director": "Frank Darabont",
    "actors": ["Tim Robbins", "Morgan Freeman"],
    "rating": 9.3,
    "awards": {"nominations": 7, "wins": 0}
  },
  {
    "title": "The Dark Knight",
    "year": 2008,
    "genres": ["Action", "Crime", "Drama"],
    "director": "Christopher Nolan",
    "actors": ["Christian Bale", "Heath Ledger"],
    "rating": 9.1,
    "awards": {"nominations": 8, "wins": 2}
  },
  {
    "title": "Pulp Fiction",
    "year": 1994,
    "genres": ["Crime", "Drama"],
    "director": "Quentin Tarantino",
    "actors": ["John Travolta", "Samuel L. Jackson"],
    "rating": 8.9
  },
])

db.movies.find()            // Find all movies

db.movies.find(     // Find Sci-Fi movies (projection to show only titles)
  {"genres": "Sci-Fi"},
  {"title": 1, "_id": 0}
)

db.movies.find(             // Find movies with rating greater than 9.0
  {"rating": {"$gt": 9.0}},
  {"title": 1, "rating": 1, "_id": 0}
)

db.movies.findOne(          // Find one movie directed by Christopher Nolan
  {"director": "Christopher Nolan"}
)

db.movies.updateOne(        // Update movie rating
  {"title": "Inception"},
  {"$set": {"rating": 8.9}}
)

db.movies.updateOne(        // Add an award to a movie
  {"title": "Inception"},
  {"$push": {"awards.wins": ["Best Visual Effects"]}}
)

db.movies.deleteOne({"title": "The Dark Knight"})  // Delete a specific movie
 
db.movies.deleteMany(       // Delete all movies from a specific year
  {"year": 1994}
)

db.movies.deleteMany({})    // Delete all movies

db.movies.insertMany([      // Insert back all movies 
  {
    "title": "The Shawshank Redemption",
    "year": 1994,
    "genres": ["Drama"],
    "director": "Frank Darabont",
    "actors": ["Tim Robbins", "Morgan Freeman"],
    "rating": 9.3,
    "awards": {"nominations": 7, "wins": 0}
  },
  {
    "title": "The Dark Knight",
    "year": 2008,
    "genres": ["Action", "Crime", "Drama"],
    "director": "Christopher Nolan",
    "actors": ["Christian Bale", "Heath Ledger"],
    "rating": 9.1,
    "awards": {"nominations": 8, "wins": 2}
  },
  {
    "title": "Pulp Fiction",
    "year": 1994,
    "genres": ["Crime", "Drama"],
    "director": "Quentin Tarantino",
    "actors": ["John Travolta", "Samuel L. Jackson"],
    "rating": 8.9
  },
  {
  "title": "Inception",
  "year": 2010,
  "genres": ["Action", "Sci-Fi", "Thriller"],
  "director": "Christopher Nolan",
  "actors": ["Leonardo DiCaprio", "Ken Watanabe"],
  "rating": 8.8
  }
])

db.movies.countDocuments({"genres": "Drama"})   // Count drama movies  

db.movies.distinct("director")          // Find all unique directors

db.movies.aggregate([ // Find top 3 directors with highest average ratings
  
  // Stage 1: Filter movies with rating >= 8.0
  {"$match": {"rating": {"$gte": 8.0}}},

  // Stage 2: Group by director
  {"$group": {
    "_id": "$director",
    "count": {"$sum": 1},
    "avg_rating": {"$avg": "$rating"},
  }},

  // Stage 3: Sort by average rating (descending)
  {"$sort": {"avg_rating": -1}},

  // Stage 4: Limit to top 3
  {"$limit": 3}
])

db.movies.drop("movies")    // Delete the collection

exit()                      // Leave MongoDB

// Bring back mongod and close it
// CTRL+C
