2

In MongoDb Shell

db.keyword.update({"state":"UT"}, {$unset:{'abc.def':1}}); 

unsets removes def from abc . However, if I do it like this :

var key = 'def'

var key1 = 'abc.'+key 
db.keyword.update({"state":"UT"}, {$unset:{key1:1}}); 

doesnt unset def.

How do I get to unset "abc.def" by passing key1 ?

1 Answer 1

5

Yes, this is how json parser works. By the standard, hash keys must be enclosed in quotes, but some parsers are too forgiving and allow you to omit them. So, this is how mongo sees your code.

db.keyword.update({"state": "UT"}, {"$unset": {"key1": 1}}); 

You can get around this problem by constructing the hash manually. Something like this:

var key = 'def';
var key1 = 'abc.' + key;

var mod = {"$unset": {}};
mod["$unset"][key1] = 1;

db.keyword.update({"state": "UT"}, mod);
Sign up to request clarification or add additional context in comments.

2 Comments

Tried it - It didnt work ... Somehow on the server side I see it still being passed as key1 only.
Thanks a lot for the prompt reply !!

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.