2014-03-27 39 views
0

我有以下對象:增量字典值MongoDB中C#驅動

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

但是,這在第二個增量語句上失敗,下面的stac ķ跟蹤:

堆棧跟蹤 - 在System.Number.StringToNumber(字符串str的NumberStyles選項,NumberBuffer &數的NumberFormatInfo信息,布爾parseDecimal) 在System.Number.ParseInt32(字符串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.訪問(Expression節點) 在MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.Visit (表達式節點) at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.VisitLambda(LambdaExpression node) 在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值)

我如何去更新一個子文檔的字典中的值?

+0

它看起來像一個解析問題。你確定你的數據庫中有一個整數嗎? – yaoxing

+0

是的,如果我做Update.Inc(「Actions.timesheetrows.Count」,1);那麼它可以正常工作,但我希望能夠通過linq動態地執行此操作 – Gazeth

回答

2

我懷疑這個行爲是否因爲MongoDB官方JIRA問題CSHARP-917而被實現。但是,您始終可以採用其他方式:

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

這看起來好像它已經解決了,直到JIRA問題得到解決,只是希望避免在任何地方都有字符串。 – Gazeth