0

I have a MongoDB database with a settings collection that I need to delete a field from by key. In the docs I see that deleteOne accepts an Object [key, value].

How can I delete it with just the key since the value is unknown?

// key - req.params.key
db.collection('settings').deleteOne(???);
AfterShotzZHD
  • 311
  • 2
  • 4
  • 15
  • Check this answer here https://stackoverflow.com/a/10146816/4782034 – Crowlex Jul 20 '19 at 06:32
  • @Crowlex This was not in the docs when I was searching around so I assumed it was not for [Node.js](https://github.com/mongodb/node-mongodb-native). Is it? – AfterShotzZHD Jul 20 '19 at 06:38

1 Answers1

2

If I understand you question correctly. You want to delete a document where a particular field exists?

If so you can use the $exists element query operator.

Let's say the field name is config.

db.collection('settings').deleteOne({config: {$exists: true}})

Here is the source documentation: https://docs.mongodb.com/manual/reference/operator/query/exists/

Kane Hooper
  • 1,531
  • 1
  • 9
  • 21