Need help with MongoDB query for date filtering in n8n

Hi everyone,

I’ve been trying for days to get a MongoDB query working properly in n8n to filter data based on dates, but I’m struggling to make it work.

I need to run this MongoDB query in n8n:

{
  "$or": [
    {
      "seats_created_at": {
        "$gte": ISODate("2025-03-23T03:38:40.000Z")
      }
    },
    {
      "seats_update_at": {
        "$gte": ISODate("2025-03-23T03:38:40.000Z")
      }
    }
  ]
}

This query should find documents where either seats_created_at or seats_update_at is greater than or equal to the specified date.

However, I’m having trouble implementing this in n8n’s MongoDB node. How do I properly format this query in n8n to handle the ISODate() function? Does the MongoDB node in n8n support this type of date query directly?

Any help or examples would be greatly appreciated!

Thank you

Welcome @renandlsantos to the community :tada:

I think you need to take a look here Date and time with Luxon | n8n Docs

maybe fromISO() would e usefull in your case

I am having the same issue, in MongoDB Compass or NodeJS these work:

{
  createdAt: {
    $gte: ISODate("2025-04-24T00:00:00.000Z")
  }
}

nodeJs:

import { MongoClient } from 'mongodb';

/*
 * Requires the MongoDB Node.js Driver
 * https://mongodb.github.io/node-mongodb-native
 */

const filter = {
  'createdAt': {
    '$gte': new Date('Thu, 24 Apr 2025 00:00:00 GMT')
  }
};

const client = await MongoClient.connect(
  'mongodb+srv://...'
);
const coll = client.db('reza-prod').collection('orders');
const cursor = coll.find(filter);
const result = await cursor.toArray();
await client.close();

In n8n I tried:

{
  "createdAt": {
    "$gte": "{{ DateTime.fromISO('2025-04-24T00:00:00.00') }}"
  }
}

That compiles to:

{   "createdAt": {     "$gte": "2025-04-24T00:00:00.000+01:00"   } }

having same kind of issue and i have tried all the variation that would return filtered documents based on dates. it is strange that plain javascript {} works on find operation but any kind of filtering based on date doesn’t work. if anyone find any solution please notify me

Dear @mohamed3nan I have attempted to use fromISO() in my query. Below is the JSON-like format query for the n8n MongoDB aggregate node:

[
  {
    "$match": {
      "memberCode": "0010019",
      "dateTimeOfCollection": {
        "$gte": "{{DateTime.fromISO('2025-01-01T00:00:00.00')}}",
        "$lt": "{{DateTime.fromISO('2025-01-06T00:00:00.00')}}"
      }
    }
  },
  {
    "$limit": 7
  }
]

The query executes successfully, but unfortunately, it does not return any results. I have also tried other queries without date filters, and they work correctly, retrieving the expected data as verified in the MongoDB shell.

I ended up adding an IF node right after my mongo query and filtered on the date field. Hope this helps!

Hello everyone,

I found the solution for the MongoDB node of n8n to filter out data based on dates, and here’s the MongoDB query which parses in n8n’s MongoDB node.

[
  {
    "$match": {
      "$and": [
        { "memberCode": "0010019" },
        {
          "$expr": {
            "$and": [
              {
                "$gte": [
                  "$dateTimeOfCollection",
                  { "$dateFromString": { "dateString": "2025-01-01T00:00:00.000Z" } }
                ]
              },
              {
                "$lt": [
                  "$dateTimeOfCollection",
                  { "$dateFromString": { "dateString": "2025-01-06T00:00:00.000Z" } }
                ]
              }
            ]
          }
        }
      ]
    }
  },
  {
    "$limit": 7
  }
]

The query executes successfully in n8n and returns the expected results using this date format. I have also tried all the types of date filters, and they work correctly, retrieving the expected data as verified in the MongoDB shell.

Thank you!