Mongoose Query to Find Matching Key Inside Array Of Objects

  express, javascript, mongodb, mongoose, node.js

I am building an Expense Tracker Application, There is a User Collection that has fields like Name, Amount, Expenses Array, Incomes Array, and So On.

My Database Is Mongo Db with Mongoose and the Server Is Written In Express.

Here Is A Screenshot Of the Database after The Values are Filled
MongoDB Database Screenshot

I am trying to Implement a Route in which The User Can Delete an Expense and After Deleting the Expense I want to update the Balance and make the Balance = Balance + Expense. I am able to Delete and Expense But Not able to Update the Balance as I do not know how to retrieve the Balance from the Deleted Expense


Here is the Delete Route :
Express delete Route


I want to add a Mongoose Method which Will Fetch the Expense Amount from the Received Expense Id and Store it in a variable and After the Expense Gets Deleted, I want to call a method to update the balance of the User in the Promise.

Here is an example of what I intend to do
Intended Output


Lets Say From the Above Database Snap, I want to delete the Expenses (Starting From 0) 1st Object Element, Name : Uber Fare, I will send the Object Id which is 6091f725403c2e1b8c18dda3 to the Server and should Expect my Balance to Increase from 48495 to 49695

Source: Ask Javascript Questions

LEAVE A COMMENT