Last active
July 27, 2021 16:19
-
-
Save dj-nitehawk/6d8e9912223eb6228b69fe792782aef4 to your computer and use it in GitHub Desktop.
update by incrementing field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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