0

我有以下对象:

[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 options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) 在 MongoDB.Driver.Linq.Utils .BsonSerializationInfoFinder.VisitGetItem(MethodCallExpression node) at 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.ExpressionVisitor1.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder。 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, Dictionary2 serializationInfoCache 的 Lambda(LambdaExpression 节点)) MongoDB.Driver.Builders.UpdateBuilder 1.Inc(Expression1 memberExpression,Int32 值)

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

4

1 回答 1

2

我怀疑是否由于 MongoDB Official JIRA issue CSHARP-917而实现了该行为。但是,您总是可以通过其他方式做到这一点:

Update.Inc(string.Format("Actions.{0}.Count", action.ToLower()), 1);
于 2014-03-27T10:09:45.777 回答