N8n AI Agent calling mongodb agregate tool with ISODates

Hello Everyone, First of all thanks for everything everyone is doing.

I’m trying to create a chatbot that will query my mongoDB database. And i have lost many days trying to make it work.

Im using local n8n docker image, local ollama docker image, and my mongodb atlas server. The llm is qwen-coder instruct 14b.

My problem starts with this query created by the llm:
{
“pipeline”: "[\n {\n "$match": {\n "company_id": "COM-19",\n "departure": {\n "$gte": { "$date": "2025-03-04T00:00:00.000-05:00" },\n "$lte": { "$date": "2025-03-04T23:59:59.999-05:00" }

The response from my mongoatlas server is empty (but there are documents) as it doesnt understand the $date as a date (i think im not an expert).

if the query is like “mongoatlas” likes (i know it works because i tested it on the mongoatlas console) :slight_smile: :
{
“pipeline”: “[\n {\n "$match": {\n "company_id": "COM-19",\n "$or": [\n {\n "status": { "$ne": "TRANSIT" },\n "departure": {\n "$gte": ISODate("2025-03-04T00:00:00.000Z"),\n "$lt": ISODate("2025-03-05T00:00:00.000Z")\n }\n },\n {\n "status": "TRANSIT",\n "arrival": {\n "$gte": ISODate("2025-03-04T00:00:00.000Z"),\n "$lt": ISODate("2025-03-05T00:00:00.000Z")\n }
The response from n8n is :
Unexpected token ‘I’, …” “$gte”: ISODate(“2”… is not valid JSON

i cannot seem to find a way to make the llm create a good $match filter with the $gte or $lte of a date. (for example documents created before a date).

can someone point me in the right direction?

1 Like