2

I had unset an array field, in mongo but it's leave the field with valué null,

the question is, how to remove it!

like this image https://i.sstatic.net/O2WiV.png

1 Answer 1

6

You could remove all null elements from the array with the $pull operation.
See docs

Code example:

db.collecton.update({condition: "x"}, {$pull: {state: null}});
Sign up to request clarification or add additional context in comments.

2 Comments

in this case, I have try like this one : db.tbl_location.update({country:"Indonesia"}, {$pull:{province:0}}); But it's not work, please check it
In the $pull condition you have to set the value that should be removed from the array not the index. So in you're case {$pull:{province:null}}. NOTE: This removes ALL null values from the array.

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.