2014-09-03 105 views
2

能有人幫我解決這個問題相關的MongoDB C#驅動程序:

我有一個從服務器中的日期自動更新的字段。但它只在更新文檔時有效。

這工作(因爲是更新()):

IMongoUpdate update = new UpdateDocument(new BsonDocument().Add("$currentDate", new BsonDocument().Add("lastModified", true) 
         .Add("lastModifiedTS", new BsonDocument("$type", "timestamp"))).Add(new BsonDocument() 
         .Add("$set", new BsonDocument().Add("altered", alteredElementValue)))); 

var updateResult = _collection.Update(Query.EQ("_id", key), update); 

這不工作(這是一個插入):

_collection.Save(bson.First().Add(new UpdateDocument(new BsonDocument().Add("$currentDate", new BsonDocument().Add("lastModified", true) 
         .Add("lastModifiedTS", new BsonDocument("$type", "timestamp")))))); 

但是,當我插入文檔它給這個錯誤:

A first chance exception of type 'MongoDB.Driver.WriteConcernException' occurred in MongoDB.Driver.dll 
Additional information: WriteConcern detected an error 
'The dollar ($) prefixed field '$currentDate' in '$currentDate' is not valid for storage.'. 
(Response was { "ok" : 1, "code" : 52, "err" : "The dollar ($) prefixed field '$currentDate' in '$currentDate' is not valid for storage.", "n" : NumberLong(0), "updatedExisting" : false }). 

難道是因爲MOngoDB只支持這種字段來更新嗎?

+1

它可能是您給出的確切錯誤響應。您不能在文檔中以'$'開頭的字段。這是爲操作員保留的,所以字段不能用這種方式命名。你有可能反過來嗎?所以你實際上是想將「當前時間戳」保存到「lastModified」字段? – 2014-09-03 05:58:13

回答