Consider a collection users with documents like:
{ "name": "Alice", "age": 30, "hobbies": ["reading", "hiking"] }What will this query return?
db.users.find({ "hobbies": "hiking" })db.users.find({ "hobbies": "hiking" })Think about how MongoDB matches array fields with a value.
MongoDB matches documents where the array contains the value directly. So it returns the full document where hobbies include "hiking".
In MongoDB's BSON format, which data type stores date and time values?
It is a special BSON type different from a string.
The BSON Date type stores date and time as milliseconds since the Unix epoch.
You want to insert a document with a nested address field. Which command is correct?
db.customers.insertOne({ name: "John", address: { city: "NY", zip: "10001" } })Remember nested documents use curly braces, not brackets or parentheses.
Nested documents are represented as objects with curly braces. Brackets are for arrays, parentheses are invalid here.
You want to speed up queries filtering by a nested field address.city. Which index type should you create?
Think about indexing a specific nested field.
Creating a single field index on the nested field address.city allows efficient queries filtering by city.
Given documents with a field tags as an array of strings, why does this query return no documents?
db.posts.find({ tags: { $eq: ["mongodb", "database"] } })Think about how MongoDB compares arrays in queries.
The $eq operator matches the entire array exactly, including order. If the stored array order differs, no match occurs.