0

Here is my efforts :

async.waterfall([
            function(nextCall){
                MongoClient.connect(url, function(err, db) {
                    if (err) throw err;

                    const dbo = db.db("testmdb");

               const criteria = {"_id":ObjectId(id)};
                    console.log("paymentInof[] ::: ",paymentInfo)
                    let obj = paymentInfo[0];
                    const query = {
                        $push:{payment:obj}                    
                    };

                    dbo.collection("Invoice").update(criteria, query);
                    db.close();
                    nextCall(null,{code:200});                                                                                                                                                                            
                });
            }
        ],function(err,results){
            if(err) return err;
            if(results.code === 200)
          console.log(chalk.blue(' ::: all done ::: '));
            next();
    }); 

Input from api explorer :

   {
  "payment":[{"transaction_at":"2018-02-12T06:04:35.279Z","paid_amount":350,"patient_id":"1233sssdd33","patient_urn":"214125","invoice_amount":700,"user":"me"}],
  "updated_by": "me"
}

Everything working fine but unable to push instead overwriting the existing object in payment array.

While from mongo shell it is working fine.

Please help me , where I am doing wrong ?

Thanks.

uday214125
  • 569
  • 1
  • 8
  • 22

1 Answers1

0

I think you need to check mongoose update upsert option.

Update options

There are several option values that can be used with an update

multi - update all records that match the query object, default is false (only the first one found is updated)

upsert - if true and no records match the query, insert update as a new record

raw - driver returns updated document as bson binary Buffer, default:false

Please check the documentation to here.

Use following code,

 async.waterfall([
          function(nextCall){
              MongoClient.connect(url, function(err, db) {
                  if (err) throw err;

                  const dbo = db.db("testmdb");

                  let criteria = {"_id": ObjectId(id)};
                  let obj = paymentInfo[0];
                  let query = { $push: { payment: obj } }

                  dbo.collection("Invoice").update(criteria, query, {upsert:true});
                  db.close();
                  nextCall(null,{code:200});                                                                                                                                                                            
              });
          }
      ],function(err,results){
          if(err) return err;
          if(results.code === 200)
        console.log(chalk.blue(' ::: all done ::: '));
          next();
  }); 

Also please check the similar type of question to here and here.

Hope this will help you!!

Santosh Shinde
  • 6,045
  • 7
  • 44
  • 68