# Mongo Node: prefiltering by dates

**URL:** <https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124>\
**Category:** Feature Requests\
**Created:** [February 1, 2021, 3:01pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124 "2021-02-01T15:01:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miquel\_Colomer](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/miquel_colomer/32/18606_2.png) [@Miquel\_Colomer](https://community.n8n.io/u/Miquel_Colomer)\
**Post date:** [February 1, 2021, 3:01pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/1 "2021-02-01T15:01:15Z")

</div>

Hi,

I am trying to limit data returned in a query Find Operation by createdAt property greater than a defined date.

I know this is possible querying directly to Mongo client, but when I try to put this in a Find Operation in a users table:  
{“active”: true, “createdAt” : { $gte : new Date(“2021-01-12T20:15:31Z”) }}

returns next error:  
ERROR: Unexpected token $ in JSON at position 33

Is there any way to filter results by date with a $gte operator?

Thanks!

---

<div class="post-metadata">

**Author:** ![mluhta](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mluhta/32/2691_2.png) [@mluhta](https://community.n8n.io/u/mluhta)\
**Post date:** [February 1, 2021, 3:26pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/2 "2021-02-01T15:26:54Z")

</div>

Try enclosing the $gte in quotes:

```auto
{"active": true, "createdAt" : { "$gte" : new Date("2021-01-12T20:15:31Z") }}

```

---

<div class="post-metadata">

**Author:** ![Miquel\_Colomer](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/miquel_colomer/32/18606_2.png) [@Miquel\_Colomer](https://community.n8n.io/u/Miquel_Colomer)\
**Post date:** [February 1, 2021, 4:03pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/3 "2021-02-01T16:03:20Z")

</div>

Hi @mluhta,

I have added this query in a Find Operation and I get

ERROR: Unexpected token e in JSON at position 43

Any ideas?

---

<div class="post-metadata">

**Author:** ![RicardoE105](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ricardoe105/32/550_2.png) [@RicardoE105](https://community.n8n.io/u/RicardoE105)\
**Post date:** [February 1, 2021, 4:19pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/4 "2021-02-01T16:19:42Z")

</div>

What mongo version are you using? Trying to test this locally.

---

<div class="post-metadata">

**Author:** ![Miquel\_Colomer](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/miquel_colomer/32/18606_2.png) [@Miquel\_Colomer](https://community.n8n.io/u/Miquel_Colomer)\
**Post date:** [February 1, 2021, 4:39pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/5 "2021-02-01T16:39:06Z")

</div>

I am using next version: 4.2.8

---

<div class="post-metadata">

**Author:** ![RicardoE105](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ricardoe105/32/550_2.png) [@RicardoE105](https://community.n8n.io/u/RicardoE105)\
**Post date:** [February 1, 2021, 8:02pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/6 "2021-02-01T20:02:43Z")

</div>

Ok, you can use an expresion like this `{{JSON.stringify({ "active": true, "createdAt": { $gte : "2021-01-12T20:15:31Z"} })}}`

---

<div class="post-metadata">

**Author:** ![Miquel\_Colomer](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/miquel_colomer/32/18606_2.png) [@Miquel\_Colomer](https://community.n8n.io/u/Miquel_Colomer)\
**Post date:** [February 1, 2021, 9:26pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/7 "2021-02-01T21:26:19Z")

</div>

Thanks @RicardoE105.

I have checked and query is returning no data.

 ![image](https://europe1.discourse-cdn.com/n8n/original/2X/d/dcd90605af94c0ca45af7a77bab4a37ce390b946.png)

If I remove “createdAt” section, data is returned correctly.

---

<div class="post-metadata">

**Author:** ![RicardoE105](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ricardoe105/32/550_2.png) [@RicardoE105](https://community.n8n.io/u/RicardoE105)\
**Post date:** [February 2, 2021, 12:03am UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/8 "2021-02-02T00:03:17Z")

</div>

Perhaps cuz there is not results that are active:true after that date?

---

<div class="post-metadata">

**Author:** ![Miquel\_Colomer](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/miquel_colomer/32/18606_2.png) [@Miquel\_Colomer](https://community.n8n.io/u/Miquel_Colomer)\
**Post date:** [February 6, 2021, 2:16pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/9 "2021-02-06T14:16:50Z")

</div>

Hi Ricardo,

There are results after date.  
I will do some more tests.

Thank you!

---

<div class="post-metadata">

**Author:** ![Davi\_Gomes](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/davi_gomes/32/6287_2.png) [@Davi\_Gomes](https://community.n8n.io/u/Davi_Gomes)\
**Post date:** [August 24, 2021, 7:11pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/10 "2021-08-24T19:11:07Z")

</div>

Hi Miquel, am I having the same problem as you? I’ve searched a lot on the internet but I haven’t found a solution. Did you manage to get around this problem? If yes, could you help me?

Thank you very much in advance.

---

<div class="post-metadata">

**Author:** ![RicardoE105](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ricardoe105/32/550_2.png) [@RicardoE105](https://community.n8n.io/u/RicardoE105)\
**Post date:** [August 24, 2021, 11:45pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/11 "2021-08-24T23:45:15Z")

</div>

Welcome to the community @Davi_Gomes

Not completely sure, but that is likely caused by the date not being a date object. If I remember correctly you have to send a Date object instance (see example below). If that is the case there is currently no way to parse the date from the `Query` box.

```auto
db.mycollection.find({
    "active": true,
    "createdAt" : {"$gte": new Date("2021-01-12T00:00.00Z")}
})

```

---

<div class="post-metadata">

**Author:** ![Davi\_Gomes](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/davi_gomes/32/6287_2.png) [@Davi\_Gomes](https://community.n8n.io/u/Davi_Gomes)\
**Post date:** [August 25, 2021, 10:00am UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/12 "2021-08-25T10:00:07Z")

</div>

> [@RicardoE105](#):
>
> Not completely sure, but that is likely caused by the date not being a date object. If I remember correctly you have to send a Date object instance (see example below). If that is the case there is currently no way to parse the date from the `Query` box

Thank you very much @RicardoE105, I am happy to be a part.

In fact your query is correct, but in the Query box when I instantiate the Date object, the query doesn’t work. I tried using JSON.stringify but it doesn’t work either because the new Date is processed and turns into a string.  
For now I had to convert the field to timestamp and make the query comparing integers. But I would like to use it as a date, I have other situations where I won’t be able to get away from it.

Thank you in advance for the return.

---

<div class="post-metadata">

**Author:** ![RicardoE105](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ricardoe105/32/550_2.png) [@RicardoE105](https://community.n8n.io/u/RicardoE105)\
**Post date:** [August 25, 2021, 10:59pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/13 "2021-08-25T22:59:02Z")

</div>

Yes, as I mentioned, that is saldy not possible from the query box. We would need to abstract the filter functionality and provide an UI. Something where you can set the field, the type and the value.

---

<div class="post-metadata">

**Author:** ![kimus](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/kimus/32/7301_2.png) [@kimus](https://community.n8n.io/u/kimus)\
**Post date:** [October 11, 2021, 3:41pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/14 "2021-10-11T15:41:06Z")

</div>

When Will this be supported?

In the MongoDB documentation there is support for [Extended JSON](https://docs.mongodb.com/manual/reference/mongodb-extended-json/) - Node driver should support this already. And so, a Date could be represented by:

```auto
{"$date":"2019-08-11T17:54:14.692Z"}

```

---

<div class="post-metadata">

**Author:** ![David\_Panart](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/david_panart/32/8913_2.png) [@David\_Panart](https://community.n8n.io/u/David_Panart)\
**Post date:** [December 7, 2021, 6:12pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/15 "2021-12-07T18:12:39Z")

</div>

Hello,

We too are facing this issues, with no apaprent solutoin, thoug it is quite a bug limiation unfortuntely ! Has anyone got a workaround / solution since ? 🙂

---

<div class="post-metadata">

**Author:** ![Mustafa\_Ali](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mustafa_ali/32/6492_2.png) [@Mustafa\_Ali](https://community.n8n.io/u/Mustafa_Ali)\
**Post date:** [December 29, 2021, 8:22am UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/16 "2021-12-29T08:22:59Z")

</div>

Hello all,

I am also facing the similar issue to filter data on the basis of date. Is there any update or work around this bug?

The query I am using is -

```auto
{"reviewDate":{"$lte": new Date({{new Date().toISOString()}})}}

```

---

<div class="post-metadata">

**Author:** ![Miquel\_Colomer](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/miquel_colomer/32/18606_2.png) [@Miquel\_Colomer](https://community.n8n.io/u/Miquel_Colomer)\
**Post date:** [December 29, 2021, 8:44am UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/17 "2021-12-29T08:44:32Z")

</div>

I usually post-filter results by date to bypass this limitation.

But if you have lots of documents, it will affect n8n performance.

---

<div class="post-metadata">

**Author:** ![ozanerturk](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ozanerturk/32/10384_2.png) [@ozanerturk](https://community.n8n.io/u/ozanerturk)\
**Post date:** [February 1, 2022, 6:46pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/18 "2022-02-01T18:46:59Z")

</div>

You can use find with `$expr` to parse date parameters with `$dateFromString` and query documents

Example query date between 2021-11-11 - 2021-12-12

```auto
{
    "$expr":{
        "$and":[
        	{"$lt":["$createdAt",{ "$dateFromString": { "dateString": "2021-12-12T12:12:12.123Z"}}]},
            {"$gt":["$createdAt",{ "$dateFromString": { "dateString": "2021-11-11T12:12:12.123Z"}}]},
        ]
    }
}

```

another example with using the output of node `Date & Time` as a parameter. do not forget to add quotes around the expresssion

```auto
{
  "$expr": {
    "$and": [
      {
        "$gt": [
          "$createdAt",
          {
            "$dateFromString": {
              "dateString": "{{$node["Date & Time"].json["lastDay"]}}"
            }
          }
        ]
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [March 12, 2022, 8:01am UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/19 "2022-03-12T08:01:25Z")

</div>

Hey @ozanerturk, thanks so much for this workaround! I have been using the Function node for such queries until now, but your approach is much more elegant.

---

<div class="post-metadata">

**Author:** ![BenW](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/benw/32/2472_2.png) [@BenW](https://community.n8n.io/u/BenW)\
**Post date:** [July 11, 2022, 6:44pm UTC](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124/20 "2022-07-11T18:44:22Z")

</div>

Note for myself: Although the field is named “createdAt”, in the query it has to be “$createdAt”. (Took one hour to figure this out) 😟

[Next page](https://community.n8n.io/t/mongo-node-prefiltering-by-dates/4124.md?page=2)
