Mathias
Mathias

Reputation: 1980

Increment DateTime in mongodb

I'm using the .net driver with C# to connect to mongodb. With mongodb it is possible to increment values directly in database:

Builders<MyObject>.Update.Inc(x => x.TotalCount, 10)

Is this also possible with dates?

Builders<MyObject>.Update.Inc(x => x.ValidUntil, new TimeSpan(1,0,0)) // add 1 hour

Obviously this does not build because the type of the field (DateTime) has to match the type of the value (TimeSpan) that is added (see docs).

Is there a different way than getting first the date from database, modify it (add 1 hour) and then update it?

Upvotes: 1

Views: 938

Answers (1)

cl0ud
cl0ud

Reputation: 1614

It looks like to me that this feature has not been implemented in mongo yet.

SERVER-3174

Seems like the only way to do this for now is to physically retrieve your document first and then add whatever time you need to and then update.

Upvotes: 2

Related Questions