How to get ID for last record affected by update/upsert?


#1

How can I get the ID of the record affected from an .update operation with upsert: true, independent from whether an update or an insert was performed?

My understanding is that the record ID will be returned when using .insert, but .update only returns a true/false.

My code:

Entity.update({
          attr1: val1,
          attr2: val2
        }, {
          $setOnInsert: {
            attr1: val1,
            attr2: val2
          }
        }, {
          upsert: true
        });

#2

AFAIK, there is no way with one query only. You need to run an additional query.