If there are documents with endDate,I want to fetch documents in a collection whose endDate+3 days is less than today through mongo query
The below query will find records whose endDate is less then today
const myCollection= DB_CONNECTION.collection(TRANSACTION);
let expiredTrans = await myCollection.find({ "endDate": { $lt: curTime}}).toArray();
I need to add 3 days to that endDate and then fetch records.
2
Answers
You need the
$dateAdd
operator, but since it is an aggregation operator, you need to work with the$expr
operator.Optional, you can replace
curTime
with$$NOW
which is a MongoDB variable to get the current time.Demo @ Mongo Playground
You can try this