0
0
MongoDBquery~5 mins

Array of embedded documents queries in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an embedded document in MongoDB?
An embedded document is a document stored inside another document as a value of a field. It helps group related data together in one place.
Click to reveal answer
beginner
How do you query for documents where an array contains an embedded document with a specific field value?
Use dot notation with the field inside the embedded document. For example, { 'arrayField.fieldName': value } finds documents where the array has an embedded document with that field value.
Click to reveal answer
intermediate
What does the $elemMatch operator do in MongoDB queries?
$elemMatch matches documents where at least one element in an array meets multiple conditions. It helps when you want to match several criteria inside the same embedded document in the array.
Click to reveal answer
intermediate
How can you update a specific embedded document inside an array in MongoDB?
Use the positional operator $ to update the first matching embedded document. For example, db.collection.updateOne({ 'arrayField.field': value }, { $set: { 'arrayField.$.fieldToUpdate': newValue } }) updates the matched embedded document.
Click to reveal answer
intermediate
What is the difference between querying with 'arrayField.field': value and using $elemMatch?
'arrayField.field': value matches if any embedded document in the array has that field value. $elemMatch lets you specify multiple conditions that must all be true in the same embedded document.
Click to reveal answer
Which MongoDB query finds documents where an array contains an embedded document with field 'score' equal to 10?
A{ 'array.$.score': 10 }
B{ 'array.score': 10 }
C{ 'array': 10 }
D{ 'array': { $elemMatch: { score: 5 } } }
What does the $elemMatch operator allow you to do?
ARemove embedded documents from an array
BMatch documents with empty arrays
CUpdate all embedded documents in an array
DMatch multiple conditions inside the same embedded document in an array
How do you update the first embedded document in an array that matches a condition?
AUse the positional operator $ in the update query
BUse $elemMatch in the update query
CUse $push operator
DUse $unset operator
Which query matches documents where an array contains an embedded document with 'score' 10 and 'grade' 'A' in the same element?
A{ 'array.score': 10, 'array.grade': 'A' }
B{ 'array': { $all: [10, 'A'] } }
C{ 'array': { $elemMatch: { score: 10, grade: 'A' } } }
D{ 'array.$.score': 10, 'array.$.grade': 'A' }
What is the result of querying { 'array.score': { $gt: 5 } }?
ADocuments where any embedded document in 'array' has 'score' greater than 5
BDocuments where all embedded documents in 'array' have 'score' greater than 5
CDocuments where 'array' contains the number 5
DDocuments where 'score' field is greater than 5 outside the array
Explain how to query for documents where an array contains an embedded document matching multiple conditions.
Think about matching several things inside one embedded document in the array.
You got /3 concepts.
    Describe how to update a specific embedded document inside an array in MongoDB.
    Remember the $ operator points to the matched array element.
    You got /3 concepts.