Hii
I have a collection orders, containing a field total_item. Total Items are calculated with sumation of all numerical values present in item. Item is present in list of object.
Now lets say that if a new item comes in listOfItems then the value of total_item should automatically update.
For example:
if my new value is { item: 5, name: “Value4”}
Then expected update in my collection should be:
Is there any way to create this kind of flow?
Because its only an example, but in my collection it contains multiple fields which are based on multiple numerical calculations and its hard to track correct data inserted through every formula.
This is just like formula field in SalesForce.
Hope this information is helpful to solve my query
Thanx for your response. But what I want is not update query. My requirement is that field should be enough efficient to automatically calculate the values based on given formula.
I apologize if my previous explanation was unclear. My concern revolves around the automatic update of the ‘total_item’ value when a new item is inserted into my list, similar to the example provided. How can I achieve this functionality in MongoDB?
I am not aware of any “formula” type field in MongoDB. May be mongoose can do it for you.
In plain mongo, you may
update the field like I shared
compute the field with a projection every time you find or aggregate
create a view with the same aggregation as #2 above
#1 is the fastest but takes more space, the computed value can be indexed, it has a high risk of becoming out of sync if documents are manipulated without your API
Okay noted. But this procedure is depended on API, and my documents are manipulated by API itself. So, I think it would better that API code should be properly written.