0
0
MongoDBquery~10 mins

Date and timestamp types in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new Date object representing the current date and time.

MongoDB
const currentDate = new [1]();
Drag options to blanks, or click blank then click option'
AISODate
BTimestamp
CDate
DTime
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Timestamp' instead of 'Date' to create a date object.
Using 'ISODate' which is a helper but not the standard Date constructor.
2fill in blank
medium

Complete the code to insert a document with a field 'createdAt' set to the current date and time.

MongoDB
db.collection.insertOne({ createdAt: [1] });
Drag options to blanks, or click blank then click option'
ATimestamp()
Bnew Date()
CISODate()
DDate()
Attempts:
3 left
💡 Hint
Common Mistakes
Using Date() without 'new' which returns a string, not a Date object.
Using Timestamp() which is a different type.
3fill in blank
hard

Fix the error in the query to find documents with a 'createdAt' date greater than January 1, 2023.

MongoDB
db.collection.find({ createdAt: { $gt: [1] } })
Drag options to blanks, or click blank then click option'
Anew Date('2023-01-01')
BTimestamp('2023-01-01')
CISODate('2023-01-01')
D'2023-01-01'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a Date object causes no matches or errors.
Using 'Timestamp' incorrectly as it requires different parameters.
4fill in blank
hard

Fill both blanks to create a query that finds documents with a 'lastModified' timestamp less than or equal to the current time.

MongoDB
db.collection.find({ lastModified: { [1]: [2] } })
Drag options to blanks, or click blank then click option'
A$lte
Bnew Date()
C$lt
DTimestamp()
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt instead of $lte changes the condition.
Using Timestamp() without 'new' or parameters is incorrect.
5fill in blank
hard

Fill all three blanks to create an update that sets the 'updatedAt' field to the current date and increments a 'version' field by 1.

MongoDB
db.collection.updateOne({ _id: id }, { [1]: { updatedAt: [2] }, [3]: { version: 1 } })
Drag options to blanks, or click blank then click option'
A$set
Bnew Date()
C$inc
D$currentDate
Attempts:
3 left
💡 Hint
Common Mistakes
Using $currentDate instead of $set with new Date().
Forgetting to increment the version field properly.