0

Why the bulk y replacing other fields if upsert is declare?

What am i doing wrong?

original collection:

   {
      _id:'xx',
      a:1
    }

process

    var bulk = collection.initializeUnorderedBulkOp();

    users.forEach(x => {

      bulk
        .find({ _id:'xx'})
        .upsert()
        .updateOne({
          b: 2
        });
    });

    bulk.execute();

result

{
  _id:'xx',
  b:2
}
Fraga
  • 1,361
  • 2
  • 15
  • 47

1 Answers1

0

Now i undestand! i need to use the $set command so it only update some fields.

var bulk = collection.initializeUnorderedBulkOp();

users.forEach(x => {

  bulk
    .find({ _id:'xx'})
    .upsert()
    .updateOne({
        $set: //CHANGED LINE
        { 
          b: 2
        }
   });

});

bulk.execute();

Now it works

{
  _id:'xx',
  a:1,
  b:2
}
Fraga
  • 1,361
  • 2
  • 15
  • 47