But if I use $$NOW and ISODate() in comparisons, only ISODate() results in a valid comparison occuring.
{
$match:
{
endDate: {$gt: '$$NOW'}
}
}
vs
{
$match:
{
endDate: {$gt: ISODate()}
}
}
Against exactly the same documents, only the $match using ISODate() returns any documents where the endDate is in the future. I thought that we could use $$NOW here as it also returns the current time as an ISODate?
The $match stage has the following prototype form:
{ $match: { <query> } }
For query we have
To specify equality conditions, use <field>:<value> expressions in the query filter document:
{ <field1>: <value1>, ... }
As an expression $$NOW gives you
A variable that returns the current datetime value. NOW returns the same value for all members of the deployment and remains the same throughout all stages of the aggregation pipeline.
As a value $$NOW gives you the string “$$NOW”. Now WOW
Luckily,
Instead, use a $expr query expression to include aggregation expression in $match.
And $now $why ISODate works? It does because ISODate is a function call on the client, so it is always sent as a value to the server so it works as well in isoNow:<value> and in isoNow:<expression> since a value is a valid expression.