I have a dictionary model like this:
const dictionarySchema = new Schema({
entry: String,
created: { type: Date, default: Date.now },
});
and I want to select this entry each month after its creation date.
How you do this ?
Something like this gives me the entries older than a month, but this is not what I want:
const conditions = {
created: { $lt: minusDays(new Date(), 30) },
}
const dicEntry = await dbs['en'].Dictionary.find(conditions);
I want a solution to select the entry every 30 days from its creation date.
2
Answers
You might need to contraing the search to the day 30 days ago at 00:00:00 and the next day at 00:00:00
As youre doing right now
you get all created 30 days ago, 31 days ago, 32 days ago, 33 days ago, etc…
you can utilize the $expr operator in MongoDB along with the $mod operator to calculate the number of days that have passed since the entry’s creation date.
hope it’s useful