[Bug]: Could not filter collection by date
Closed this issue · 9 comments
Version
Version 0.0.3
App
- Cursor
- Windsurf
- VSCode
- VSCode Insiders
- Claude Desktop
- Other
Affected Models (if applicable)
- Claude 3.5 Sonnet
- Claude 3.7 Sonnet
- GPT-4a
- o4-mini
- Other
Bug Description
The MCP Failed to filter by date (Found 0 documents in the collection hidden) despite queries being correct
_Now let me check the date range of all evaluations to see what dates we actually have:
[MongoDB (MCP Server)]
Interesting! The data shows there ARE evaluations up to August 25, 2025. Let me now specifically count those from August 2025:_
{
"collection": "hidden"
"database": "db",
"query": {
"created": {
"$gte": {
"$date": "2025-08-01T00:00:00.000Z"
},
"$lt": {
"$date": "2025-09-01T00:00:00.000Z"
}
}
}
}
Output
Found 0 documents in the collection hidden...
Model attempted several queries but couldn't get any document back, it knows documents that match the condition exist (because it used aggregate tool with date expression operators)
{
"collection": "hidden",
"database": "db",
"pipeline": [
{
"$addFields": {
"year": {
"$year": "$created"
},
"month": {
"$month": "$created"
}
}
},
{
"$match": {
"year": 2025,
"month": 8
}
},
{
"$count": "august_count"
}
]
}
This is puzzling. The aggregation shows a max date of August 25, 2025, but I can't find those records. Let me sort by created date descending to see the most recent ones:...
I can see from the sorted results that there are records with dates like "2025-08-25T10:56:44.046Z"...
Can someone test this to confirm?
Thanks for opening this issue. The ticket MCP-206 was created for internal tracking.
Hey! Thank you for raising this.
Is the field it is filtering on inside the database of type Date or of type string? It may be coming across some mismatch there.
Its of type date, specifically ISODate.
when i query directly using mongodb compass it works by wrapping the date with ISODate("date_here") but the mcp cannot pass it in.
Try an attempt on a mocked data to further test this.
Hi @wael7721 ,
it seems that the issue is with the model not understanding that the field is an actual Date, not a String. If you tell the model to specifically use $date for this specific field, or saying that is a date, does it work?
We are aware that some models are pretty bad understanding dates so we are investigating how to give them more clues to choose the right format for the field, but we are not likely to have a solution in the short term.
Here's an example using $date
User:
can you show me last month's applicants? use '$date'
{
"collection": "hidden",
"database": "x",
"filter": {
"created": {
"$gte": {
"$date": "2025-08-01T00:00:00.000Z"
},
"$lt": {
"$date": "2025-09-01T00:00:00.000Z"
}
}
}
}
Output:
Found 0 documents in the collection `hidden`:
User:
im sure there are applicants last month
I apologize for the inconvenience. It's possible I'm using the wrong date field or there's another issue with my query. Let me try to retrieve a few of the most recent `hidden` to better understand the data structure.
{
"collection": "hidden",
"database": "x",
"limit": 5,
"sort": {
"created": -1
}
}
Output:
Found 5 documents in the collection `hidden`:
You are right! My apologies. It seems I was mistaken.
Here are the applicants from last month:
user1 - Applied on 2025-08-25
user2 - Applied on 2025-08-06
It appears my previous query was incorrect. I will be more careful in the future.
I knew it might have been an issue with the model which is why i used langchain-mcp-adapters and manually tested the tools using various parameters but they didnt work, btw this issue also happened to me with ObjectIds too
User:
who is the user with id USERID?
{
"collection": "hidden",
"database": "x",
"filter": {
"_id": {
"$oid": "USERID"
}
}
}
Error running find: unknown operator: $oid
I am sorry, I cannot fulfill your request. It seems I am having trouble with the query. I will try again.
{
"collection": "hidden",
"database": "x",
"filter": {
"_id": "USERID"
}
}
Output: Found 0 documents in the collection `hidden`:
I believe this is an issue with the tool itself (find and count)not the models
Hi @wael7721 , you are right.
So what I've found is that this issue happens when we use the stdio transport, as we were using a custom implementation of the transport for supporting EJSON. Due to some inconsistencies and using the { relaxed: true } option of EJSON.parse, that can lead to type info loss, $dates and other more complicated types where not parsed properly.
I've opened a PR that should fix this and also make it work properly for the HTTP Transport, that had other issues with $dates.
Thanks a lot for helping us debug this, it's really appreciated!
Happy to assist! Thanks!
Released! The fix is part of v1.0.1.
The corresponding JIRA ticket has been automatically closed.