MongoDB插入" $ currentDate"领域

时间:2014-09-03 05:53:16

标签: mongodb datetime

有人可以帮我解决与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 个答案:

答案 0 :(得分:0)

是的,你是对的。这是一个错误。 MongoDb不支持$currentDate的{​​{1}}:https://jira.mongodb.org/browse/SERVER-13695