Last active
July 28, 2023 16:49
-
-
Save muratbaseren/07ac745863bcf76e4a2617eadbf60688 to your computer and use it in GitHub Desktop.
MongoDB CRUD with Core Console Application in C#
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
MongoDB CRUD with Core Console Application in C# |
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
public class Tasko | |
{ | |
public ObjectId _id { get; set; } | |
public string Detail { get; set; } | |
public bool IsDone { get; set; } | |
public DateTime DueDate { get; set; } | |
} |
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
var connectionString = "mongodb://localhost:27017"; | |
var client = new MongoClient(connectionString); | |
IMongoDatabase db = client.GetDatabase("TodoDB"); | |
await db.CreateCollectionAsync("Tasks"); |
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
IMongoCollection<Tasko> col = db.GetCollection<Tasko>("Tasks"); | |
var tasks = col.Find(new BsonDocument()).ToList(); |
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
Tasko newTask = new Tasko() | |
{ | |
Detail = "Deneme", | |
DueDate = DateTime.Now, | |
IsDone = false | |
}; | |
await col.InsertOneAsync(newTask); |
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
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); |
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
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