Home > Enterprise >  How do I delete_many filtering by two conditions?
How do I delete_many filtering by two conditions?

Time:02-03

I'm using pymongo to try and delete_many records where it matches two conditions but it's not deleting any records:

db.hashes.delete_many(
  {"highlights": {"$exists": "false"}}, {"last_pulled": {"$lte": purgeDate}} ## This works in mongosh as expected, but not pymongo.
)

i've tried to put the two conditions in an $and array but that didn't work either:

db.hashes.delete_many(
  {"$and": [{"highlights": {"$exists": "false"}}, {"last_pulled": {"$lte": purgeDate}}]}
)

When I filter by just the one purgeDate condition it does delete records as expected:

db.domains.delete_many({"last_pulled": {"$lte": purgeDate}})

but I need both conditions to filter by.

p.s. this is how I'm setting purgeDate:

from datetime import datetime, timedelta

N_DAYS_AGO = 90
purgeDate = datetime.now() - timedelta(days=N_DAYS_AGO)

Any ideas? TIA!

CodePudding user response:

You have a subtle misplacement of braces, combined with the wrong boolean expression. The sytntax you want is:

db.hashes.delete_many(
  {"highlights": {"$exists": False}, "last_pulled": {"$lte": purgeDate}}
)
  • Related