I’m having an issue where findOneAndUpdate doesn’t return a document in my Trigger. findOneAndUpdate is updating the query document correctly but I need the updated document _id returned to add to a different document.
The lines below with <---- are referring to my issue.
// document info
const currManagerId = changeEvent.documentKey._id;
const currManagerDoc = changeEvent.fullDocument;// ---------- add manager to season query = { 'leagueId' : currManagerDoc.leagueId, 'year' : currManagerDoc.years[0] }; update = { '$push' : { 'managers' : currManagerId }, '$inc' : { 'info.numManagers' : 1 } }; options = { 'upsert': false, 'returnNewDocument' : true }; const currSeasonDoc = seasons.findOneAndUpdate(query, update); // <---- TODO not returning document... but updating document fields correctly console.log(currSeasonDoc._id); // <---- undefined... // ---------- add season to manager query = { 'leagueId' : currManagerDoc.leagueId, '_id' : currManagerId }; update = { '$push' : { 'seasons' : currSeasonDoc._id }, // <---- TODO field ignored since currSeasonDoc._id is undefined '$inc' : { 'info.numSeasons' : 1 } // <---- this field gets updated correctly }; options = { 'upsert': false, 'returnNewDocument' : false }; managers.findOneAndUpdate(query, update, options);
Any tips on what might be wrong here? Or if there is a different function that is better suited for this application? Thanks.