'I want to update "tot_amt" field in my MongoDB document during the runtime. I am using spring boot as framework?

The database schema :-

{
"Id":"string",
"uname":"string",
"products":[
  {
    "prodname":"string",
    "quantity":"int",
    "price":"double"
  }],
"tot_amt":"double",
}

I want to set tot_amt=quantity*price during the runtime. I am relatively new to MongoDB abd Spring Boot, is there a set way to do this?

My Cart.java file

import lombok.Data;

@Data
@Document("cart")
public class Cart {
    
    @Id
    public String Id;
    
    @Indexed(unique=true)
    public String uname;
    
    public List<Product>products;
    public double tot_amt;

}

Product.java file

package com.example.demo.modell;

import lombok.Data;

@Data
public class Product {

    public String prodname;
    public int quantity;
    public double price;
}

CartRepository.java

@Repository

public interface CartRepository extends MongoRepository<Cart,String>{
    
    @Query("{uname:?0}")
    Optional<Cart> findByName(String name); 
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source