I am trying to create a Expense tracker application using SpringBoot, MonogDB and ReactJS
I have a collection called “userexpense” which as three fields “id”,”email” and “expenses”
``{
"id":"123",
"email":"[email protected]",
"expenses":[
{
"taskid":"xyz",
"amount" : "90",
"description : "vada pav",
"category" : "food"
},
{
"taskid":"qpr", "amount" : "900","description : "train","category" : "transport"
}
]
}`
“expenses” is an array of objects which holds expenses of individual users.
I want to perform update operation on element of expenses array.
updates like changing the amount and description of a particular expense based its taskid.
How can i do this?
github repo:
https://github.com/Sumit-Rodge/spring-boot-expense-api
This is what i have tried
` Query query = new Query();
Criteria criteria1 = Criteria.where("id").is(id);
Criteria criteria2 = Criteria.where("expenses").elemMatch(Criteria.where("id").is(taskid));
query.addCriteria(new Criteria().andOperator(criteria1, criteria2));
Update update = new Update().pull("Expenses" , new BasicDBObject("id",taskid));
mongoTemplate.update(query,update,UserExpense.class);
`