Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Last active Jul 27, 2021
Embed
What would you like to do?
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