0

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.

6
  • "So, I reverted to filtering by a specific date." - what did that query look like, and what was returned? Commented May 28, 2024 at 17:25
  • Can you try to do a query with $toDate like this? Commented May 28, 2024 at 17:27
  • updated_at_value = 1634569547000 query = { 'updated_at': updated_at_value } collection.find(query).limit(10) And here's what I got: MongoDB query: {'updated_at': 1634569547000} No documents found for the specified updated_at value Commented May 28, 2024 at 17:53
  • No, I'll go give it a try Commented May 28, 2024 at 17:54
  • Thank you so much! That helped! Now I'll try to work with ranges. Thanks!! Commented May 28, 2024 at 18:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.