Updating a list of documents in mongodb

Asked

Viewed 422 times

0

I’m trying to update a list of objects similar to this:

var lista = [ { _id: 57ea29361b297553c99d702f,
    nome: 'Meu nome',
    __v: 0 },
  { _id: 57ea29371b297553c99d7031,
    nome: 'Meu outro nome',
    __v: 0 } ]

model.update({}, lista)
        .then(function (result) {
            console.log(result);
        }, function (error) {
            console.log(error);
        });

but give me that back:

{ Okay: 0, n: 0, nModified: 0 }

if you pass only one of the list it works

model.update({}, result[0])...

{ Okay: 1, nModified: 1, n: 1 }

  • I’m not sure but it won’t be necessary {multi: true} in the arguments ? https://docs.mongodb.com/manual/reference/method/db.collection.update/#multi-parameter

  • already tried model.update({}, list, { multi: true }), does not work.

  • I think it’s only good for equal changes for all documents

  • You want to update what? The model? If this list of your example is the new names for each document, you will need an update for each doc. Type: lista.forEach(function(item){ model.update({_id: item._id}, {nome: item.nome}).then(...); });

  • @sergiopereira works :), but it would be nice to have something from mongodb that already does this, can put as an answer.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.