If I understand your question corectly then your chosen comparison field (ce
) contains the number of seconds since the epoch. If so, then you cannot directly compare that with a Javascript Date
value.
Instead, you'll have to convert the Javascript Date
object's time
value to a number of seconds and then compare against that value.
For example:
var threeMonthsAgo = new Date();
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
var threeMonthsAgoInSeconds = threeMonthsAgo.getTime() / 1000
db.collection.find({
ce: { $gte: threeMonthsAgoInSeconds }
})
This conversion of millis to seconds is subject to some inaccuracy and if this inaccuracy is unacceptable then I think the solution is to store ce
as the number of milliseconds since the epoch.
If ce
stored the number of millis then it could be directly compared with the value of the calculated "3 months ago" Javascript date.
However, the above example does not require you to convert ce
into millis. All I'm saying is that comparing a seconds value with a calculated value which is derived from millis introduces some minimal potential for inaccuracy. It's possible that this won't matter in your use case but if it does then the only way to resolve it is to compare like with like i.e. millis against millis.