I have a collection of ISO dates stored in MongoDB as strings, like so:
{ "date": "2014-12-12T03:33:33.333Z" },
{ "date": "2014-12-13T03:33:33.333Z" }
In the console, I can query these perfectly using
{ "date": ISODate("2014-12-12T03:44:00.000Z") }
However, I'm using the NodeJS driver, and I cannot use ISODate
. I have found several questions pertaining to this problem on here, but none of the proposed solutions seem to work. For instance:
// These does not find any matches
db.find({ "date": new Date("2014-12-12T03:44:00.000Z") })
db.find({ "date": { '$eq': '2014-12-12T03:44:00.000Z' } })
db.find({ "date": { '$eq': new Date('2014-12-12T03:44:00.000Z') } })
//This throws an error stating $date is not an operator
db.find({ "date": { '$date': '2014-12-12T03:44:00.000Z' } })
Why are these queries failing?
Edit: Here's another sample, straight from the database:
{
"_id": "5a7e88f34b5916723589183f",
"date": "2014-12-12T03:42:00.000Z",
"granularity": 180
}
EDIT 2: This query produces the following error MongoError: $dateFromString requires that 'dateString' be a string, found: date with value 2014-12-12T03:44:00.000Z
async loadCandle(date, granularity) {
date = date + ''; //Aded to ensure date is a string, but still get the error.
var candle = await this.data.collection('dates').findOne(
{ $expr :
{$eq :
[
{$dateFromString : {dateString : "$date"}},
new Date("2014-12-12T03:33:33.333Z") //Normally would pass in the `date` variable here
]
} });