0

I have a collection with single DOC which I use as "source of truth" and want to be able to remove & add values to it:

{
    "_id" : ObjectId("61012ada8d2ccb252be87551"),
    "language" : "english",
    "gui-ipv6" : "disable",
    "gui-certificates" : "enable",
    "gui-custom-language" : "disable",
    "gui-display-hostname" : "disable",
    "admin-https-ssl-versions" : "tlsv1-1 tlsv1-2 tlsv1-3",

}

I want to be able to delete a line, say language for example. Whenever I try to do this, it removes entire DOC since my query matches entire DOC e.g:

db.my_collection.remove({'language': {$exists:true}})

Any ideas on how to solve this?

Thank you.

sngx
  • 185
  • 1
  • 10
  • Does this answer your question? [How to remove a field completely from a MongoDB document?](https://stackoverflow.com/questions/6851933/how-to-remove-a-field-completely-from-a-mongodb-document) – turivishal Jul 28 '21 at 12:33

1 Answers1

1

Use operator unset for delete field https://docs.mongodb.com/manual/reference/operator/update/unset/

db.my_collection.update(
   {},
   { $unset: { language: "" },
   false, true }
)
Kitsor
  • 144
  • 2
  • 10
  • Hmm that didn't quite work: > db.fgvm_security_posture.update({}, { $unset: {'language': ''}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > – sngx Jul 28 '21 at 11:16
  • my bad, had a nested dict so had to specify full object. – sngx Jul 29 '21 at 11:53