-1

Below is the my JSON request data and I want to update currency and subAmount. I tried but it did not updating properly.

Here is my code:

{
    "mid":"5032219",
    "description" : "Sample description",
    "currency" : "INR",
    "amount" : {
        "subAmount":"20"
    }
}

I tried using below code, passing the params values are - {currency=AUG, amount={"subAmount":"20.00"}} public void updateInvoiceModel(InvoiceModel model, Map param) {

        MongoCollection<Document> collection = database.getCollection("invoice");
        BasicDBObject searchQuery = new BasicDBObject("invoiceNumber", model.getInvoiceNumber());
        BasicDBObject updateFields = new BasicDBObject();
        for (Entry<String, Object> entry : param.entrySet()) {
            updateFields.append(entry.getKey(), entry.getValue());
        }
        BasicDBObject setQuery = new BasicDBObject();
        setQuery.append("$set", updateFields);
        collection.updateOne(searchQuery, setQuery);

} Kindly let me know if anyone have a solution.

pavan kumar
  • 11
  • 1
  • 4

1 Answers1

0

Please use the following:

db.collection_name.update(
   { _id: mid },
   { $set:
      {
         "currency" : "INR", "amount" : { "subAmount":"20" }
      }
   }
)
Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
Abdulla Thanseeh
  • 9,438
  • 3
  • 11
  • 17