Metabase fails on new ISODate() mongo native query

OS: Ubuntu 16.04
Metabase version: 0.29.3

I am running an aggregation pipeline for mongo, where I’m trying to look at all the datapoints in the past X days.
Due to the nature of the data, I need to run it in a JSON mongo query. The step that is failing looks like this:

{"$match": 
{"time_stamp": 
    {"$lt": new ISODate(), 
     "$gte": new ISODate(new ISODate().setDate(new ISODate().getDate()-30))}}}

and I am getting the following error:

Unrecognized token 'new': was expecting 'null', 'true', 'false' or NaN at [Source: 
java.io.StringReader@2a8753d; line: 37, column: 21]

The problem is obviously the first call to new ISODate().

I have found several (https://github.com/metabase/metabase/pull/4132; https://github.com/metabase/metabase/issues/3170) other github bug reports regarding this and they appear as fixed and merged, yet I cannot do it…

Am I doing something wrong, or is it actually a feature that’s not (yet) supported?

I’m not a MongoDB user myself, but searching for ISODate here gives several examples of MongoDB ISODate() queries without the new keyword. What happens if you drop the news in your query?

Good point and I have tried that too:

{  
"$match": 
{"time_stamp": 
    {"$lt": ISODate(), 
     "$gte": ISODate(ISODate().setDate(ISODate().getDate()-30))
    }
}
}

and I get the following error:

Unexpected character (’.’ (code 46)): was expecting comma to separate Array entries at
[Source:java.io.StringReader@13c6fd05; line: 37, column: 48]

It does not interpret “.” correctly, and that seems to happen with operations ("-") as well.
However, using just ISODate() does seem to work if I only have this matching code:

{"$lt": ISODate()}

but this is kind of useless, since I need a way to set dates relative to the present moment.
I’m sure I could hardcode the dates every time I need the query, but that defeats the purpose of making it automatic…

I am also having the same problem.
Did you find any solution for this?

I have a similar problem.

You can store any function in Mongodb. I used this to create a function that returns the current date and time and tried to call it from the metabase.
https://docs.mongodb.com/manual/tutorial/store-javascript-function-on-server/

However, in order to execute any additional functions, you need to run db.loadServerScripts() to load the function.
I couldn’t do this with metabase, so I gave up.

Translated with www.DeepL.com/Translator (free version)

It still doesn't work.
I need it to set the date of today at midnight like

"dateString": ISODate().getFullYear() + "-" + (ISODate().getMonth() + 1) + "-" + ISODate().getDate() + "T00:00:00"

1 Like