Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muratbaseren/07ac745863bcf76e4a2617eadbf60688 to your computer and use it in GitHub Desktop.
Save muratbaseren/07ac745863bcf76e4a2617eadbf60688 to your computer and use it in GitHub Desktop.
MongoDB CRUD with Core Console Application in C#
MongoDB CRUD with Core Console Application in C#
public class Tasko
{
public ObjectId _id { get; set; }
public string Detail { get; set; }
public bool IsDone { get; set; }
public DateTime DueDate { get; set; }
}
var connectionString = "mongodb://localhost:27017";
var client = new MongoClient(connectionString);
IMongoDatabase db = client.GetDatabase("TodoDB");
await db.CreateCollectionAsync("Tasks");
IMongoCollection<Tasko> col = db.GetCollection<Tasko>("Tasks");
var tasks = col.Find(new BsonDocument()).ToList();
Tasko newTask = new Tasko()
{
Detail = "Deneme",
DueDate = DateTime.Now,
IsDone = false
};
await col.InsertOneAsync(newTask);
tasks = col.Find(new BsonDocument()).ToList();
filter = Builders<Tasko>.Filter.Eq("_id", ObjectId.Parse("5a8fd6698d2ae55a40829b72"));
var mytask = (await col.FindAsync(filter)).FirstOrDefault();
mytask.IsDone = true;
await col.ReplaceOneAsync(filter, mytask);
var filter = Builders<Tasko>.Filter.Eq("_id", ObjectId.Parse("5a8fd6698d2ae55a40829b72"));
await col.DeleteOneAsync(filter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment