0

I need mongoose to run a query that finds documents older than an amount of time described in milliseconds found in the config folder. This amount of time is stored like this,

keys.anAmountOfMilliseconds

Thus far, I've been able to pull all documents created in between NOW and FIVE minutes as described in this link. **MongoDB: Only fetch documents created in the last 24hrs? **, but reverse engineering this query has so far been beyond me.

Any help would be amazing!! Thanks!

ThirdGhostHand
  • 377
  • 5
  • 19
  • 1
    "Older than" means "Less than" `$lt` and "Newer than" means "Greater than" `$gt`, optionally varying to `$lte` or `$gte` depending on what you mean. Not much to engineer here as "lesser" or "greater" is just *reversing* the expression used. – Neil Lunn Apr 25 '19 at 02:16

1 Answers1

3

I think you will want to use the less-than operator for building your query. To build off the answer you linked I think it will need to look something like this

db.getCollection("COLLECTION_NAME").find({"createdAt":{$lt:new Date(Date.now() - 5*60 * 1000)}})

Or for your case

db.getCollection("COLLECTION_NAME").find({"createdAt":{$lt:new Date(Date.now() - keys.anAmountOfMilliseconds)}})

Haven't had a chance to test but that should return all documents older than the amount of time you specify.