Given a collection events with a date field of type Date, which query returns all documents where the date is after January 1, 2023?
Use the $gt operator with a Date object to compare dates.
Option A correctly uses new Date() to create a Date object and compares with $gt. Option B compares with a string, which does not work as expected. Option C uses ISODate which is a shell helper and works in the MongoDB shell. Option D compares with a string timestamp, which is not reliable.
Which statement correctly describes the difference between MongoDB's Date and Timestamp types?
Think about how MongoDB uses Timestamp internally for replication.
Date stores a standard date and time value. Timestamp is a special type used mainly for replication and contains a time and an increment to ensure uniqueness.
Which option correctly creates a MongoDB Timestamp with time 1672531200 and increment 1?
Use the new keyword with two numeric arguments for time and increment.
Option D uses the correct constructor syntax. Option D misses new. Options C and D use invalid argument formats.
You want to find all documents in logs collection with createdAt between 2023-01-01 and 2023-01-31. Which query is most efficient assuming createdAt is indexed?
Use inclusive start and exclusive end dates for ranges to avoid overlap.
Option B uses inclusive start and exclusive end with correct Date objects, which is efficient and avoids missing documents on boundary. Option B excludes 2023-01-01 and misses documents on 2023-01-31. Option B uses ISODate which may not be available in all drivers. Option B compares strings, which is less efficient and unreliable.
Given documents with eventDate stored as Date type, why does this query return no results?
db.events.find({ eventDate: { $eq: '2023-03-15T00:00:00Z' } })Check the data types used in the query and stored in the documents.
The query compares the eventDate field (a Date object) to a string. MongoDB does not convert types automatically, so no documents match. The correct approach is to compare with a Date object.