I need to increment a value that is stored inside a dictionary, the structure of my object is (basically) the following:
public class OrderDetails : EntityBase
{
public int OrderId { get; set; }
public Dictionary<int, int> TotalViewsPerUser { get; set; }
}
As this object can be competed between several requests, I can't just bring the object to the server, increment the value and then save it again.
So, what I need to do is increment the value of the variable TotalViewsPerUser
, the dictionary key is the UserId
.
If there is no key, I need to insert the key and start with the value 1.
I looked for some examples, of the use of "Inc" and I found the following example, but, I could not make it work, because Mongo would not insert the key.
What I did is:
var update = Builders<OrderDetails>.Update
.Inc($"totalViewsPerUser.{userId}.v", 1);