Dan
Dan

Reputation: 183

Updating an array element in nedb

I know this has been answered before, but for some reason none of the solutions has helped me. So I'd like to ask for some help and see if you can spot what I'm doing wrong. I have nedb running and a .db file that looks something like this (removed a few attributs to make it clearer):

{
    "name":"Haj",
    "members": [{"id":"x","name":"x"}],
    "shops": [{"id":"123","name":"shopname","category":"xyz"}],
    "_id":"XXXXX"
}

What I want to do is edit one attribute in one shop, for example the name. I've tried this that seems to be the solutions I see a lot. But for some reason it doesn't work for me.

router.put('/vote', function(req, res) {
    //hardcoded testing
    req.db.groups.update(
      {"_id": "XXXXX", "shops.id": "123"},
      {$set:{"shops.$.name":"why wont you work"}}, 
      function(err, items){
        res.contentType('application/json');
        res.send('{"success":true}');
    });
});

It works if I change an attribute that's not within the shops array (like "name":"Haj"). Using:

{$set:{"name":"this works"}}

I hope someone is able to help me :)

Upvotes: 3

Views: 1874

Answers (1)

Dan
Dan

Reputation: 183

Thanks! As pointed out be Neil Lunn it's not supported. We made a workaround by retrieving the data, and then by code modify it, and then update the document with the modified data.

Upvotes: 1

Related Questions