1

I need to update collection and return updated value. My code is:

const someFunction = async () => {
  const { value } = await dataStore.customerSettings.findOneAndUpdate(
            { organizationId }, 
            { $push: { archivedSuppliers: archivedSupplier } }
          );
  return value;
}

Problem is that the value is equal to collection object before update. Why is it? And how to get updated value?

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Nikita Polevoy
  • 319
  • 5
  • 17
  • Does this answer your question? [Mongoose: findOneAndUpdate doesn't return updated document](https://stackoverflow.com/questions/32811510/mongoose-findoneandupdate-doesnt-return-updated-document) and other similar [question](https://stackoverflow.com/questions/35626040/findoneandupdate-used-with-returnnewdocumenttrue-returns-the-original-document) – turivishal May 09 '21 at 05:14

1 Answers1

4

http://mongodb.github.io/node-mongodb-native/4.0/interfaces/findandmodifyoptions.html#returnoriginal

findOneAndUpdate(filter, update, options, callback)

pass option

returnOriginal: false

When false, returns the updated document rather than the original. The default is true.


await dataStore.customerSettings.findOneAndUpdate(
            { organizationId }, 
            { $push: { archivedSuppliers: archivedSupplier } },
            { returnOriginal: false } // add here
);
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107