D
D
Denis Denis2016-07-18 20:51:23
MongoDB
Denis Denis, 2016-07-18 20:51:23

How to change object in mongodb collection?

Hello everyone)
Suppose there is a user object, and an array of objects in the profile.

var user = {
_id:1
profile:{admin:false,
subscriptions:[
{id:1, name:'name1'}, {id:2, 'name:2'}
]}
}

Suppose you need to change subscriptions with id == 1
It is clear that you can overwrite the entire array, but you want to do it beautifully, like $pull/$push.
It should be something like this
db.users.update(
  {
    "_id" : 1,
    "suscriptions.id" : 1
  },
  {
    $set :
    {
        "suscriptions.$" : {
          "name" : "new name",
        }
    }
  }
);

but for some reason it doesn't work. save, help)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
napa3um, 2016-07-19
@golovewkin

stackoverflow.com/questions/10522347/mongodb-updat...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question