1

I'm using Mongo's native node driver. For an upsert like:

collection.update(query, setData, { upsert: true }, callback);

Is there a way to determine if the upsert did an insert or an update? With Mongo shell you can get back WriteResult.nUpserted to determine this, but I'm unsure how to get that info back from the node native driver. http://docs.mongodb.org/manual/reference/method/WriteResult/#WriteResult.nUpserted

Thanks.

Erik Olson
  • 5,807
  • 1
  • 18
  • 17

1 Answers1

1

You should be able to find out by checking the third argument passed to your callback:

collection.update(query, setData, {upsert: true}, function(err, nAffected, raw) {
  if (err) throw err;
  console.dir(raw);
  // raw will contain updatedExisting and the inserted item _id (if applicable)
});
mscdex
  • 104,356
  • 15
  • 192
  • 153