Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Last active July 27, 2021 16:19
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/6d8e9912223eb6228b69fe792782aef4 to your computer and use it in GitHub Desktop.
Save dj-nitehawk/6d8e9912223eb6228b69fe792782aef4 to your computer and use it in GitHub Desktop.
update by incrementing field
using MongoDB.Entities; //https://mongodb-entities.com
using System;
using System.Threading.Tasks;
namespace TestApplication
{
public class Message : Entity
{
public string Content { get; set; }
public int Upvotes { get; set; }
}
public static class Program
{
private static async Task Main()
{
await DB.InitAsync("test");
var msg = new Message { Content = "hellow world", Upvotes = 0 };
await msg.SaveAsync();
await DB.Update<Message>()
.Match(m => m.ID == msg.ID)
.Modify(b => b.Inc(m => m.Upvotes, 1))
.ExecuteAsync();
var res =await DB.Find<Message>()
.Match(m => m.ID == msg.ID)
.ExecuteAsync();
Console.WriteLine("Upvotes: " + res[0].Upvotes.ToString());
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment