Reputation: 442
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 ?
Upvotes: 2
Views: 1625
Reputation: 230461
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);
Upvotes: 5