Write MONGODB code to list all the movies that Tom Cruise is in:
db.movies.find({ actors: "Tom Cruise" })
Student Exercise:
Extend this to using regex and wildcards, $gt, $lt
Correlate between the collections: Write db shell code to list all movies with a female actress
To list all movies with a female actress, you can use MongoDB's $lookup aggregation pipeline stage to join the movies collection with the actors collection based on the actors field.
db.movies.aggregate([
{
$lookup: {
from: "actors",
localField: "actors",
foreignField: "name",
as: "actorsInfo"
}
},
{
$unwind: "$actorsInfo"
},
{
$match: {
"actorsInfo.gender": "female"
}
}
])
This code first performs a $lookup stage to join the movies collection with the actors collection.
The from field specifies the collection to join with,
localField specifies the field in the movies collection to match on (actors),
foreignField specifies the field in the actors collection to match on (name),
as specifies the name of the new field to add to the documents in the movies collection (actorsInfo).
The $unwind stage then flattens the actorsInfo field to create one document for each actor in the actorsInfo array.
Finally, the $match stage filters the results to only include documents where the actorsInfo.gender field is "female". This will return all movies that have at least one female actress.
Write Mongoose code to do all steps
(1) import the actors and movies collections from text files
(2) find movies with actor Tom Cruise
(3) do correlate between the collections with mongoose code to list all movies with a female actress
Here's the Mongoose code to do all of these steps:
To fix the error, you need to remove the callback function and use async/await instead. The updated code should look like this:
console.log('Movies with female actresses:', moviesWithFemaleActresses);
} catch (err) {
console.error(err);
}
})();
By using async/await and wrapping the code in an async function, you can properly handle the promise-based operations.
This code first connects to the MongoDB database with Mongoose.
Then, it defines Mongoose schemas for the actors and movies collections.
Next, it reads the data from the actors.txt and movies.txt files, parses the JSON data, and inserts the data into the corresponding collections using Mongoose's insertMany() method.
After that, it uses Movie.find() to find all movies that have the actor "Tom Cruise".
Finally, it uses Movie.aggregate() with the $lookup stage to join the movies collection with the actors collection, and the $match stage to filter the results to only include movies with female actresses.
Next Steps: Using this Template code: Implement your Project:
Write your Project’s Datastore. Upload it to Atlas.
Create a Web Server front end using Express.js. Use routing end points and web forms using the methods from MAD 6135 to allow Web Browser Access (CRUD) to your Datastore to implement your Project’s Use Cases.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (