Question

I have the following object:

[BsonId]
public ObjectId Id { get; set; }
public string Area { get; set; }
public DateTime Date { get; set; }
public int MethodCalls { get; set; }
public Dictionary<string, ActionStats> Actions { get; set; }

The ActionStats object looks like this:

public string Action { get; set; }
public int Count { get; set; }
public long TotalDuration { get; set; }
public Dictionary<int, int> Hourly { get; set; }

I want to be able to update the values in the using a FindAndModify like this:

Update<Statistic>.Inc(x => x.MethodCalls, 1).Inc(x => x.Actions[action.ToLower()].Count, 1);

However this fails on the second increment statement with the following stack trace:

Stack Trace -- at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.VisitGetItem(MethodCallExpression node) at MongoDB.Driver.Linq.ExpressionVisitor1.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.VisitMember(MemberExpression node) at MongoDB.Driver.Linq.ExpressionVisitor1.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.VisitLambda(LambdaExpression node) at MongoDB.Driver.Linq.ExpressionVisitor1.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo(Expression node, Dictionary2 serializationInfoCache) at MongoDB.Driver.Builders.UpdateBuilder1.Inc(Expression1 memberExpression, Int32 value)

How do I go about update the dictionary values of a subdocuments?

Was it helpful?

Solution

I doubt if the behavior is implemented because of the MongoDB Official JIRA issue CSHARP-917. However, you can always do it another way:

Update.Inc(string.Format("Actions.{0}.Count", action.ToLower()), 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top