0
0
MongoDBquery~10 mins

Many-to-many with references in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find all books that a specific author wrote by matching the author's ID.

MongoDB
db.books.find({ authors: [1] })
Drag options to blanks, or click blank then click option'
A"authorId789"
B"authorId456"
C"authorId123"
D"authorId000"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong author ID string.
Not putting the author ID in quotes.
Trying to match the entire authors array instead of an element.
2fill in blank
medium

Complete the code to add a new author ID to a book's authors array using the $push operator.

MongoDB
db.books.updateOne({ _id: bookId }, { $push: { authors: [1] } })
Drag options to blanks, or click blank then click option'
AnewAuthorId
B"newAuthorId"
C{ newAuthorId }
D["newAuthorId"]
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the author ID.
Using an array or object instead of a string.
Using the wrong update operator.
3fill in blank
hard

Fix the error in the aggregation pipeline stage to lookup authors for each book.

MongoDB
{ $lookup: { from: "authors", localField: "authors", foreignField: [1], as: "authorDetails" } }
Drag options to blanks, or click blank then click option'
A"_id"
B"id"
C"authorId"
D"authors"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong field name for foreignField.
Confusing localField and foreignField.
Using a field that does not exist in the authors collection.
4fill in blank
hard

Fill both blanks to create a query that finds authors who wrote a book with a specific title.

MongoDB
db.authors.find({ _id: { $in: db.books.findOne({ title: [1] }).[2] }) })
Drag options to blanks, or click blank then click option'
A"The Great Adventure"
Bauthors
CauthorIds
Dwriters
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong field name for author IDs in the book document.
Not quoting the book title string.
Using a field name that does not exist.
5fill in blank
hard

Fill all three blanks to create an aggregation pipeline that joins books with authors and filters books published after 2010.

MongoDB
[ { $match: { year: { $[1]: [2] } } }, { $lookup: { from: "authors", localField: "authors", foreignField: [3], as: "authorInfo" } } ]
Drag options to blanks, or click blank then click option'
Agt
B2010
C"_id"
Dgte
Attempts:
3 left
💡 Hint
Common Mistakes
Using $gte instead of $gt when strictly greater is needed.
Not quoting the year number correctly.
Using wrong field names in $lookup.