【问题标题】:Increment Dictionary Value in MongoDB C# Driver在 MongoDB C# 驱动程序中增加字典值
【发布时间】:2014-03-27 09:36:34
【问题描述】:

我有以下对象:

[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 样式,NumberFormatInfo 信息) 在 MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.VisitGetItem(MethodCallExpression 节点) 在 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(表达式节点) 在 MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(表达式节点) 在 MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.VisitLambda(LambdaExpression 节点) 在 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) 在 MongoDB.Driver.Builders.UpdateBuilder1.Inc(Expression1 memberExpression,Int32 值)

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

【问题讨论】:

  • 它看起来像一个解析问题。你确定你的数据库中有一个整数吗?
  • 是的,如果我这样做 Update.Inc("Actions.timesheetrows.Count", 1);然后它工作正常,但我希望能够通过 linq 动态地做到这一点

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


【解决方案1】:

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

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

【讨论】:

  • 这看起来已经解决了,直到 JIRA 问题得到修复,只是想避免到处都有字符串。
猜你喜欢
  • 2023-02-10
  • 2013-04-03
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2021-12-07
  • 2019-05-27
  • 2016-03-13
  • 2012-12-09
相关资源
最近更新 更多