Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating an array element in nedb

Tags:

mongodb

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 :)

like image 654
Dan Avatar asked Dec 13 '25 01:12

Dan


1 Answers

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.

like image 178
Dan Avatar answered Dec 16 '25 04:12

Dan