mongodb delete if exists code example
Example 1: mongodb remove all from collection
db.collection.remove({})
Example 2: mongodb delete all documents
# To remove all documents in a collection, call the remove method with an empty query
# document {}. The following operation deletes all documents from the bios collection:
db.bios.remove( { } )
Example 3: mongodb delete document where field exists
db.users.remove({
"$or": [
{ "firstName": { "$exists": false } },
{ "lastName": { "$exists": false } }
]
})