0

I'm using the mongodb package on my Nodejs app. I try to remove an nested object. It work when I put a nested object. It does not work when I put the string into the condition and I don't understand why. Can someone help me?

That doesn't work:

var tmp = 'records.' + j + '.domains';
records.update({'email': '<removed>'}, {'$pull': {tmp: {'_id': req.params.dn}}});

That work:

records.update({'email': '<removed>'}, {'$pull': {'records.1.domain': {'_id': req.params.dn}}});

2 Answers 2

4

You should use the following syntax:

var tmp_key = 'records.' + j + '.domains';
var tmp_value = {'_id': req.params.dn};
var query = {};
query[tmp_key] = tmp_value;
records.update({'email': '<removed>'}, {'$pull': query});
Sign up to request clarification or add additional context in comments.

Comments

1

Because you cannot use a variable as key in object literal. Instead construct the object dynamically using obj[tmp] = nested_obj.

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.