Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Created March 12, 2020 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj-nitehawk/b71e0a143422188f7ec39fa4ad9c68cb to your computer and use it in GitHub Desktop.
Save dj-nitehawk/b71e0a143422188f7ec39fa4ad9c68cb to your computer and use it in GitHub Desktop.
update with aggregation pipeline stage
using MongoDB.Entities;
using MongoDB.Entities.Core;
using System;
namespace StackOverFlow
{
public class UnitTravelHistory : Entity
{
public long JobDuration { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
public static class Program
{
private static void Main()
{
new DB("test", "localhost");
new UnitTravelHistory
{
Start = DateTime.UtcNow.AddDays(-1),
End = DateTime.UtcNow
}.Save();
DB.Update<UnitTravelHistory>()
.Match(_ => true)
.WithPipelineStage("{$set:{JobDuration:{$subtract:['$End','$Start']}}}")
.ExecutePipeline();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment