How do I query a document in a MongoDB collection using a key that I don’t know the value of? JS

I want to basically query a document in a collection and then delete it, but the problem is – I only know the key, not the value of that key. (I actually do but I am setting this up for my bigger project)

What I am trying to do:

await client.connect();
const db = client.db('testing')
console.log(`Connected to database ${db.databaseName}`)

const collection = db.collection('test1')
const allInCollection = await collection.find().toArray()

await collection.deleteOne({ id })

But for some reason, it gives me the error:

ReferenceError: id is not defined

How can I do this?

P.S this is my first ever SO post 🙂


your query is


you can find all doc that contains key of key and if you want to delete all docs that contains key with any value you could deleteMany