1

This Is An Expense Tracker Application

I'm Trying to Make An Update Route where The User Can Update the Value of An Expense

Heres A Snapshot Of My Database Model

For Example, I want to update the First Expense Name from Bike to Bike Rental & Amount from 250 to 500

And similarly, make a generic endpoint where the user can update any expense whenever necessary

Here A Snapshot of My Express Get Route & The Patch route which I want to fill

enter image description here

The ObjectID of the Entire Collection which is 607bbb07e1ebb63a3033af15 will be sent to the backend from the Frontend.

I am using Express, Mongoose.

1 Answer 1

1

You can run update on the model you have created like this:

const filter = {_id :"(*User Id Here*)", "Expenses._id": "(*Expense Id Here*)"}; //check the type of _id if this doesn't work
const query = {$set: {"Expenses.$.Name" : "value"}};

User.update(filter, query);

Or,

User.update({_id :"ObjectId(pass id)", "Expense._id" : ObjectId'pass id'}, {'$set': {'Expenses.$.Name': 'name update'});

Similarly, You can do this for any key in the object provided that the key exists in it. Now this will check the

Sign up to request clarification or add additional context in comments.

1 Comment

The Above Query is getting executed however It Isn't getting updated in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.