I need help. I'm having trouble selecting data from MongoDB with a specified date value.
I'm using Python. The date is stored in Unix format. Directly specifying the date does not result in any output. So, I wrote a query that would output the first 10 date values. In the log, I see entries like this
updated_at value: {"$date": 1634569547000}
updated_at value: {"$date": 1573048632000}
...
updated_at value: {"$date": 1536225766000}
Now that I see the values, I'm trying to select at least those documents that match the obtained date value.
updated_at_value = 1634569547000
logger.info(f"Querying documents with updated_at value: {updated_at_value}")
query = {
'updated_at': {'$eq': {"$date": updated_at_value}}
}
logger.info(f"MongoDB query: {query}")
try:
cursor = collection.find(query).limit(10)
batch = []
When I run the code, I see the result in the log:
Querying documents with updated_at value: 1634569547000
MongoDB query: {'updated_at': {'$eq': {'$date': 1634569547000}}}
No documents found for the specified updated_at value
I've tried different approaches, for example, writing the query like this:
query = {
'updated_at': updated_at_value
}
The result is the same - documents with that date are not found. I've also tried searching by range - the same thing. So, I reverted to filtering by a specific date. I'm just learning MongoDB and don't know all the nuances. I'm clearly doing something wrong. Maybe I misunderstand the log output. But it seems to me that I see the date in Unix format. And I'm developing based on that. I'm asking for help. Maybe someone has encountered something similar or just knows what my mistake is.
$toDatelike this?