增加MongoDB C#驱动程序中的字典值

时间:2014-03-27 09:36:35

标签: c# mongodb mongodb-.net-driver findandmodify

我有以下对象:

[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; }

ActionStats对象如下所示:

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

我希望能够使用FindAndModify更新这些值:

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

但是,在第二个增量语句中,使用以下堆栈跟踪失败:

  

堆栈跟踪 - 在System.Number.StringToNumber(String str,NumberStyles选项,NumberBuffer&amp; number,NumberFormatInfo info,Boolean parseDecimal)   在System.Number.ParseInt32(String s,NumberStyles样式,NumberFormatInfo信息)   在MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.VisitGetItem(MethodCallExpression节点)   在MongoDB.Driver.Linq.ExpressionVisitor 1.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.ExpressionVisitor 1.访问(表达式节点)   在MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(表达式节点)   在MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.VisitLambda(LambdaExpression节点)   在MongoDB.Driver.Linq.ExpressionVisitor 1.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo(Expression node, Dictionary 2 serializationInfoCache)   在MongoDB.Driver.Builders.UpdateBuilder 1.Inc(Expression 1 memberExpression,Int32 value)

如何更新子文档的字典值?

1 个答案:

答案 0 :(得分:2)

我怀疑是否因为MongoDB官方JIRA问题CSHARP-917而实施了该行为。但是,你总是可以用另一种方式做到:

Update.Inc(string.Format("Actions.{0}.Count", action.ToLower()), 1);