vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

How to use findOneAndUpdate with option upsert : true ? #214

Open bv99 opened 5 years ago

bv99 commented 5 years ago

So I want to save document with insert or update if exist, and figured out that findOneAndUpdate with opt {upsert:true} maybe could, but it encounter error when save(). How is proper use of this method? here is my code :

Mdel.upsert = async (where, data, cb) => {
        const start = new Date();
        const existdt = await Mdel.findOneAndUpdate(where, data, {
            upsert: true
        }).find();
        console.log('exisdata', existdt);
        await existdt.save();
        console.log('speed of upsert', new Date() - start);
        cb(existdt);
    };

and this is the error msg:

(node:21468) UnhandledPromiseRejectionWarning: TypeError: existdt.save is not a function warning.js:18 at Function.Mdel.upsert (D:\app\botkorter\src\dbase.js:46:17)