mk2015
mk2015

Reputation: 213

MongoDB declare key by Javascript Value

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".

Upvotes: 0

Views: 35

Answers (1)

adeneo
adeneo

Reputation: 318192

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});

Upvotes: 2

Related Questions