Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Mongodb deleteMany or dump with specified date

Maybe this subject often question to ask, but I not find what I want it. I have a collection with keys and values like this:

{
  _id: ObjectId("6142f3a47245125aef7ffcc0"),
  addTime: '2021-09-16 14:35:00',
  editTime: '2021-09-16 14:35:00'
}

I want to dump before August 2021. What syntax do I have to use? I trying in roboT and mongo shell, before dump I try find first.

db.collections.find({addTime:{$lte:new Date("2021-09-01")}})

and the result is

Fetched 0 record(s) in 3714ms
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can make use of the $expr operator to temporarily convert the string to a date-time value and then perform your query.

db.collection.find({
  "$expr": {
    "$lte": [
      {
        "$dateFromString": {
          "dateString": "$addTime",
          "format": "%Y-%m-%d %H:%M:%S",  // <- Your date time format in string
          "onNull": new Date("9999-09-01"),  // <- Default calue if query key doesnt exist (Dont modify this value, unless required)
        }
      },
      new Date("2021-09-01"),  // <- Your datetime query value (can also be `ISODate` format)
    ],
  }
})

Mongo Playground Sample Execution

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!