Query MongoDB using ObjectId by Date Range

get timestamp using MongoDB id

Today this article will see how to write a Query MongoDB using ObjectId by Date Range to get records based on timestamp or date range.

Timestamp details could be useful for many other reasons, It does help in getting the timestamp when the records got added to the MongoDB document the first time. Specifically, when your schema does not contains the timestamp property explicitly.

Today in this article, we will cover below aspects,

Please note that ObjectId is the 12-byte element consisting of a timestamp value, a random value, and an incremental counter.

MongoDB _id field as ObjectId

MongoDB uses the _id field as ObjectId this Id has few behavioral characteristics and it is explained below, A 4-byte timestamp is a UNIX timestamp.

It represents the ObjectId’s creation timestamp when a document is inserted into Mongo collection first time, measured in seconds since the Unix epoch. for more details, please visit here

Getting started

ObjectId Date greater than query

Command

{ <field Name>: { $gt: ObjectId.fromDate( new ISODate("Date here"))}}

 

OR

 { <field Name>: { $gt: ObjectId.fromDate( new Date("Date here"))}}   

Example:

> db.employee.find({_id: {$gt: ObjectId.fromDate( new Date("2021-01-01") )  } });

{ "_id" : ObjectId("617f1b826cbcd5cf388e44f7"), "Name" : "John", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "NY", "Country" : "USA", "States" : "NY", "Zip" : "12345", "order" : [ "name1", "name2" ] }
{ "_id" : ObjectId("61ec0d419e5a92743c245375"), "Name" : "Meery", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "NY", "Country" : "USA", "States" : "", "Zip" : "12345-0000" }
{ "_id" : ObjectId("61ec0d5e9e5a92743c245376"), "Name" : "Mohn", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "NY", "Country" : "USA", "States" : "", "Zip" : "45678-1234" }
{ "_id" : ObjectId("61ec0d789e5a92743c245377"), "Name" : "alex", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "TEST", "Country" : "USA", "States" : "OH" }
{ "_id" : ObjectId("62ac091058c3f35ca07859a9"), "Name" : "sr", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "NY", "Country" : "USA", "States" : "NY", "Zip" : "12345", "order" : [ "name1", "name2" ] }

ObjectId Date Less than query

Command

{ <field Name>: { $lt: ObjectId.fromDate( new ISODate("Date here"))}}

Example

> db.employee.find({_id: {$lt: ObjectId.fromDate( new Date("2022-01-01") ) } });

{ "_id" : ObjectId("617f1b826cbcd5cf388e44f7"), "Name" : "John", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "NY", "Country" : "USA", "States" : "NY", "Zip" : "12345", "order" : [ "name1", "name2" ] }

ObjectId Date greater than and less than query

Command

{<FieldName>: {$lt: ObjectId.fromDate( new Date("2022-05-01") ), $gt: ObjectId.fromDate( new Date("2021-01-01") ) } });

Example:

{_id: {$lt: ObjectId.fromDate( new Date("2022-05-01") ), $gt: ObjectId.fromDate( new Date("2021-01-01") ) } });

> db.employee.find({_id: {$lt: ObjectId.fromDate( new Date("2022-05-01") ),  $gt: ObjectId.fromDate( new Date("2021-01-01") )  } });

{ "_id" : ObjectId("617f1b826cbcd5cf388e44f7"), "Name" : "John", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "NY", "Country" : "USA", "States" : "NY", "Zip" : "12345", "order" : [ "name1", "name2" ] }
{ "_id" : ObjectId("61ec0d419e5a92743c245375"), "Name" : "Meery", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "NY", "Country" : "USA", "States" : "", "Zip" : "12345-0000" }
{ "_id" : ObjectId("61ec0d5e9e5a92743c245376"), "Name" : "Mohn", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "NY", "Country" : "USA", "States" : "", "Zip" : "45678-1234" }
{ "_id" : ObjectId("61ec0d789e5a92743c245377"), "Name" : "alex", "Ssn" : BinData(6,"AQN8hvdzPErPuPBzVdYmjdUCdQEKWLhy6gifgAykIemBsI06Ps+w9IzaZ7sRO8tyh+EfxODhxpmES4u7g10EnZsvXWeUUASeTYsWVb8GMbxUig=="), "City" : "TEST", "Country" : "USA", "States" : "OH" }

Query MongoDB using ObjectId date – MongoDB Atlas UI

If you are using MongoDB Atlas, please use the below command to perform the date range query using ObjectId.

Command:

 {  $expr: { $lte: [{"$toDate":"$<field name>"}, ISODate("Date here")]} }


Example

 {  $expr: { $lte: [{"$toDate":"$_id"}, ISODate("2022-01-01")]} }  

mongodb objectid to timestamp, mongodb query by objectid timestamp, mongodb objectid to string, mongodb timestamp,

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *