need to create a mongoose transaction wrapper which will pass session as parameter to functions that actually do query operations to create or update
Use https://www.npmjs.com/package/mongoose-transactions
https://mongoosejs.com/docs/transactions.html
const createUsers = async (users = []) => { const session = await UserModel.startSession(); try { await session.withTransaction(() => { return UserModel.create(users, { session: session }).then((res) => { logger.info(`[Start-Up] Created ${res.length} users`); }, (rej) => { logger.error(`[Start-Up] Failed to create users - ${rej}`); throw new Error(rej); }); }); } catch (err) { throw new Error('Failed to create Users', err); } finally { session.endSession(); }};