简体   繁体   English

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

[英]Increment Dictionary Value in MongoDB C# Driver

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: ActionStats对象如下所示:

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: 我希望能够使用FindAndModify更新这样的值,如下所示:

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.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.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.ExpressionVisitor 1.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo(Expression node, Dictionary 堆栈跟踪 - 在MongoDB.Driver.Linq.Utils的System.Number.ParseInt32(String s,NumberStyles样式,NumberFormatInfo信息)处的System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer&number,NumberFormatInfo info,Boolean parseDecimal) MongoDB.Driver.Linq.ExpressionVisitor上的.BsonSerializationInfoFinder.VisitGetItem(MethodCallExpression节点)1.在MongoDB.Driver.Linq.Uins.BsonSerializationInfoFinder中的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 (表达式节点)的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在MongoDB.Driver.Linq.Linq.Uins.BsonSerializationInfoFi的1.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit(Expression node) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo(Expression node, Dictionary 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) at MongoDB.Driver.Builders.UpdateBuilder 1.Inc(Expression 1 memberExpression, Int32 value) 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) 1.Inc(Expression 1 memberExpression,Int32 value)

How do I go about update the dictionary values of a subdocuments? 如何更新子文档的字典值?

I doubt if the behavior is implemented because of the MongoDB Official JIRA issue CSHARP-917 . 我怀疑是否因为MongoDB官方JIRA问题CSHARP-917而实施了该行为。 However, you can always do it another way: 但是,您可以始终以另一种方式执行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM