I'm trying to run this Mongo shell query from a linux bash. The problem is that I can't check if a date in Mongo is greater than a "date" stored in a bash script variable.
This runs fine:
counter=$(mongo --quiet dbName --eval 'db.dbCollection.find({"updatedAt":{"$gt":ISODate("2019-02-01T00:00:00.000Z")}}).count()')
And the "counter" var has the expected value. But I can't use a fix value for the date. So, I created a var to store a date.
DATE2=$(date -d '1 minute ago' "+%Y-%m-%dT%H:%M:%S.%3NZ");
This date var has the expected value. I can see that using a simple printf.
Now, the modified code, using the DATE2 instead of a fix date, won't work because the query returns "0" when it should return "1".
counter=$(mongo --quiet dbName --eval 'db.dbCollection.find({"updatedAt":{"$gt":"$DATE"}}).count()')
I've tried to modify the code above, using IsoDate() and other things, but nothing seems to work.
You have to put the $DATE2
inside the single quote as below to get the required results as below:
counter=$(mongo --quiet dbName --eval 'db.dbCollection.find({"updatedAt":{"$gt":new ISODate("'$DATE'")}}).count()')
I have tried with new ISODate
but will work without it also. For more help regarding the date usage click here
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments