Search code examples
mongodbdistinctdelete-record

How to delete records found with "distinct" in MongoDB?


I'm new to MongoDB, so I'm aware this is a really simple question, but I've looked everywhere and I can't find an answer? Basically I'm trying to find all records within a collection that contain the word "delete" and delete them. To get the records, I'm using

 db.collection.distinct("delete")

(correct me if I'm wrong there). But how would I go about actually deleting those? I've tried creating a variable out of that (i.e.

 var query = db.collection.distinct("delete")

) and then executing

db.collection.remove(query)

but the records are still there? Any help would be great!


Solution

  • Assuming that you're trying to delete all docs that contain a delete field:

    db.collection.remove({delete: {$exists: true}});