0
0
MongoDBquery~20 mins

Many-to-many with references in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Many-to-many Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find all books written by author with _id 'A1'

Given two collections authors and books with many-to-many references, which query returns all books written by the author with _id 'A1'?

MongoDB
db.books.find({ authors: 'A1' })
Adb.books.find({ author_id: 'A1' })
Bdb.authors.find({ books: 'A1' })
Cdb.books.find({ authors: 'A1' })
Ddb.books.find({ 'authors._id': 'A1' })
Attempts:
2 left
💡 Hint

Books store an array of author IDs in the authors field.

🧠 Conceptual
intermediate
1:30remaining
Why use references for many-to-many relationships in MongoDB?

What is the main reason to use references instead of embedding documents for many-to-many relationships in MongoDB?

ATo avoid data duplication and keep data consistent across collections
BBecause MongoDB does not support embedding arrays
CTo improve query speed by storing all data in one document
DBecause references automatically enforce foreign key constraints
Attempts:
2 left
💡 Hint

Think about data duplication and consistency when the same data appears in multiple places.

📝 Syntax
advanced
2:30remaining
Correct aggregation to lookup authors for each book

Which aggregation pipeline correctly joins books with authors to get author details for each book?

MongoDB
db.books.aggregate([
  { $lookup: {
      from: 'authors',
      localField: 'authors',
      foreignField: '_id',
      as: 'authorDetails'
  }}
])
Adb.books.aggregate([{ $lookup: { from: 'authors', localField: 'authorIds', foreignField: '_id', as: 'authorDetails' }}])
Bdb.books.aggregate([{ $lookup: { from: 'authors', localField: '_id', foreignField: 'authors', as: 'authorDetails' }}])
Cdb.books.aggregate([{ $lookup: { from: 'authors', localField: 'authors', foreignField: 'id', as: 'authorDetails' }}])
Ddb.books.aggregate([{ $lookup: { from: 'authors', localField: 'authors', foreignField: '_id', as: 'authorDetails' }}])
Attempts:
2 left
💡 Hint

Match localField with the array field in books and foreignField with the key in authors.

🔧 Debug
advanced
2:00remaining
Why does this query return no results?

Given books documents with authors as an array of ObjectIds, why does this query return no results?

db.books.find({ authors: '507f1f77bcf86cd799439011' })
ABecause the <code>authors</code> field is an embedded document, not an array
BBecause the query compares string to ObjectId, types must match
CBecause the query should use <code>$in</code> operator
DBecause the <code>authors</code> field is missing in all documents
Attempts:
2 left
💡 Hint

Check the data type of the authors array elements and the query value.

optimization
expert
3:00remaining
Best index to optimize many-to-many author-book queries

To optimize queries that find all books by a given author ID stored in the authors array field, which index is best?

ACreate a multikey index on the <code>authors</code> field in the <code>books</code> collection
BCreate a unique index on the <code>_id</code> field in the <code>authors</code> collection
CCreate a compound index on <code>authors</code> and <code>title</code> fields in <code>books</code>
DCreate a text index on the <code>authors</code> field in <code>books</code>
Attempts:
2 left
💡 Hint

Think about indexing array fields for efficient membership queries.