0
0
MongoDBquery~5 mins

Arrays in documents in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an array in a MongoDB document?
An array in a MongoDB document is a list of values stored in a single field. It can hold multiple items like numbers, strings, or even other documents.
Click to reveal answer
beginner
How do you store multiple phone numbers for a user in MongoDB?
You store multiple phone numbers as an array inside the user's document, for example: { "phoneNumbers": ["123-4567", "987-6543"] }.
Click to reveal answer
intermediate
How can you query documents where an array contains a specific value?
You can query using the field name and the value inside the array, like: { "tags": "mongodb" } to find documents where the 'tags' array contains 'mongodb'.
Click to reveal answer
beginner
What happens if you add a document with an empty array field?
The document stores the field with an empty array, meaning the field exists but has no items. This is useful to show the field is expected but currently empty.
Click to reveal answer
intermediate
Can arrays in MongoDB hold different data types together?
Yes, arrays in MongoDB can hold mixed data types like strings, numbers, and even nested documents all in the same array.
Click to reveal answer
How do you represent multiple values in a single field in MongoDB?
AUsing multiple fields
BUsing an array
CUsing a string with commas
DUsing a number
Which query finds documents where the 'colors' array contains 'red'?
A{ colors: 'red' }
B{ colors: ['red'] }
C{ colors: { $eq: 'red' } }
D{ colors: { $in: ['red'] } }
Can MongoDB arrays contain nested documents?
AOnly if the document is small
BNo
CYes
DOnly if the array is empty
What does an empty array field in a document mean?
AThe document is invalid
BThe field does not exist
CThe field is null
DThe field exists but has no items
Is it possible to store different data types in the same array in MongoDB?
AYes
BNo
COnly numbers and strings
DOnly documents
Explain how arrays are used in MongoDB documents and give a simple example.
Think about how you store a list of items like phone numbers or tags.
You got /3 concepts.
    Describe how to query documents where an array contains a specific value.
    Remember you can query by just specifying the value inside the array field.
    You got /3 concepts.