books with documents containing an array field reviews of embedded documentsJump into concepts and practice - no test required
books with documents containing an array field reviews of embedded documentsbooks collection with embedded reviewsbooks and insert these two documents exactly:{ title: "Learn MongoDB", reviews: [{ reviewer: "Alice", rating: 5 }, { reviewer: "Bob", rating: 3 }] }{ title: "Mastering Databases", reviews: [{ reviewer: "Charlie", rating: 4 }, { reviewer: "Alice", rating: 2 }] }Use db.books.insertMany() with an array of two objects. Each object must have a title string and a reviews array of embedded documents with reviewer and rating.
minRating and set it to 4 to use as the minimum rating threshold for queries.Use const minRating = 4 to create the variable.
minRatinghighRatedBooks that finds all documents in books where the reviews array contains at least one embedded document with rating greater than or equal to minRating. Use the $elemMatch operator.Use db.books.find({ reviews: { $elemMatch: { rating: { $gte: minRating } } } }) and assign it to highRatedBooks.
Alice gave a rating below minRatingaliceLowRatings that finds all documents in books where the reviews array contains an embedded document with reviewer equal to "Alice" and rating less than minRating. Use the $elemMatch operator.Use db.books.find({ reviews: { $elemMatch: { reviewer: "Alice", rating: { $lt: minRating } } } }) and assign it to aliceLowRatings.
Which MongoDB query operator is used to match documents where at least one element in an array of embedded documents meets multiple conditions?
Which of the following is the correct MongoDB query syntax to find documents where the comments array contains an embedded document with author equal to "Alice"?
{ comments: { ? } }Given the collection posts with documents like:
{ _id: 1, comments: [ { author: "Bob", likes: 5 }, { author: "Alice", likes: 3 } ] }What will the query db.posts.find({ comments: { $elemMatch: { author: "Alice", likes: { $gt: 2 } } } }) return?
Identify the error in this MongoDB query to find documents where reviews array has an embedded document with rating greater than 4 and verified true:
db.collection.find({ reviews: { rating: { $gt: 4 }, verified: true } })You have a collection orders with documents like:
{ _id: 1, items: [ { product: "Pen", qty: 10 }, { product: "Notebook", qty: 5 } ] }Write a query to find orders where items contains a product "Pen" with quantity at least 10, and also a product "Notebook" with quantity at least 5.
Which query correctly finds such orders?