1

How do you delete one value in MongoDB as opposed to an entire key?

For example I have a key "hello" that contains the following data:

> db.Data.find({'id': 'hello'}, {'_id':0}).pretty()
{
        "id" : "hello",
        "Foo" : BinData(0,"1="),
        "Bar" : BinData(0,"1="),
        "Baz" : BinData(0,"1=="),
}

Now If I try to filter for a value "Foo" in my delete statement it does not delete.

> db.fileData.deleteOne({"id": "hello", "Foo": 0})
{ "acknowledged" : true, "deletedCount" : 0 }

I just want to delete one value "Foo" not the entire key "hello", how do you do this?

1 Answer 1

1

Demo - https://mongoplayground.net/p/sJV0IXBjwwz

Use $unset

db.fileData.updateOne(
   { id : "hello" }, // query
   { $unset: { Foo: "" } } // update $unset will remove Foo
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.