Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB $date is not working #11751

Open
asosahn opened this issue Nov 15, 2024 · 2 comments
Open

MongoDB $date is not working #11751

asosahn opened this issue Nov 15, 2024 · 2 comments
Labels
in linear Issue or PR has been created in Linear for internal review

Comments

@asosahn
Copy link

asosahn commented Nov 15, 2024

Bug Description

I am trying to send a mongoDB query using { "$date" : "2024-11-08T00:00:00Z" }, but it is not working; if I put the query on Mongo compass, it works fine.

To Reproduce

Create a query like this
aggregate or find
[{ "$match": { "created_at": { "$gte": { "$date": "2020-01-01T08:23:14.000Z" } } } }, { "$limit": 10 }]

Expected behavior

returning the expected data; the node does not return anything.

Operating System

n8n cloud

n8n Version

1.67.1

Node.js Version

NA

Database

SQLite (default)

Execution mode

main (default)

@Joffcom
Copy link
Member

Joffcom commented Nov 15, 2024

Hey @asosahn,

We have created an internal ticket to look into this which we will be tracking as "GHC-447"

@Joffcom Joffcom added the in linear Issue or PR has been created in Linear for internal review label Nov 15, 2024
@my-opencode
Copy link
Contributor

my-opencode commented Nov 19, 2024

Hi there fellow n8n user. @asosahn

I encountered the same use case. There is a work around with $expr and $toDate.

In the code example I provide, please keep in mind that in my workflows, the documentDate field can be a JSDate or a string.

Here is an example of code that I use to fetch claims between two dates:

{
"anyOtherField": <any value or expr>,
"$expr": { "$and": [
  { "$lte": ["$claimPeriodStartDate", { "$toDate": "{{ $json.documentDate instanceof Date ? $json.documentDate.toISOString() : $json.documentDate }}"}]},
  {"$gte": ["$claimPeriodEndDate", { "$toDate": "{{ $json.documentDate instanceof Date ? $json.documentDate.toISOString() : $json.documentDate }}"}]}
}

If you check the source code, you'll see that the query is parsed as a strict JSON to reconstruct an object, it's not using the strict JSON mode therefore no $date or $oid.
I've had to modify my local version to replace $oid and $date by function calls for inserts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in linear Issue or PR has been created in Linear for internal review
Projects
None yet
Development

No branches or pull requests

3 participants