0

I want to set a value as a mongodb array key.

my value for is

var value = "arrayKey" and i want to update a mongodb collection and set this value as a key.

collection.update(
    {
        "schraenke.name": schrank.name
    }, 
    {
        $push: {
            value: {
                "test": test
            },
        }
    }
});

When i try it the key is value and not "arraKey".

0

1 Answer 1

2

That's because keys are literal when written that way, you can create the object first and use bracket notation to use the dynamic key, then pass in the object, something like

var value = "arrayKey"
var push  = {};

push[value] = { "test": test };

collection.update({"schraenke.name": schrank.name }, {$push: push});
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.