using System; using System.Collections.Generic; using MongoDB.Bson; using MongoDB.Driver; namespace MongoDBApp_CSharpCore { class Program { static void Main(string[] args) { MongoDBTest(); Console.WriteLine("Exiting main"); Console.ReadKey(); // Pause } private static async void MongoDBTest() { MongoClient client = new MongoClient(); IMongoDatabase database = client.GetDatabase("mydb"); IMongoCollection collection = database.GetCollection("test"); Create("1","buy something","BASE",collection); Create("2","sell","BASE",collection); Create("3","close down","BASE",collection); Console.WriteLine("Reading all the documents"); await collection.Find(new BsonDocument()).ForEachAsync(d => Console.WriteLine(d)); Read("1",collection); Update("2","sold out",collection); Delete("1",collection); DeleteAll(collection); } private static async void Create(String id, String info, String comp, IMongoCollection collection) { String time = DateTime.Now.ToString(); BsonDocument doc = new BsonDocument { { "event_id", id }, { "event_time", time }, { "event_info", info }, { "event_company", comp } }; Console.WriteLine("----CREATE SUCCESS!----"); await collection.InsertOneAsync(doc); } private static async void Read(String id, IMongoCollection collection) { BsonDocument doc = await collection.Find(Builders.Filter.Eq("event_id", id)).FirstOrDefaultAsync(); if (doc == null) { Console.WriteLine("No document found for {0}", id); } else if (doc.Contains("event_info")) { Console.WriteLine("----READ SUCCESS!----"); Console.WriteLine("Finding document: {0} ", doc); } else { Console.WriteLine("Error: info field did not exist in document"); } } private static async void Update(String id, String newinfo, IMongoCollection collection) { BsonDocument doc = await collection.Find(Builders.Filter.Eq("event_id", id)).FirstOrDefaultAsync(); if (doc == null) { Console.WriteLine("No document found for {0}", id); } else if (doc.Contains("event_info")) { Console.WriteLine("(The document is ready for updating)"); } else { Console.WriteLine("Error: info field did not exist in document"); } UpdateResult updated; updated = await collection.UpdateOneAsync(Builders.Filter.Eq("_id", doc["_id"]), Builders.Update .Set("event_info", newinfo)); doc = await collection.Find(Builders.Filter.Eq("event_id", id)).FirstOrDefaultAsync(); Console.WriteLine("----UPDATE SUCCESS!----"); Console.Write("Changed document now contains {0}\n", doc); } private static async void Delete(String id, IMongoCollection collection) { DeleteResult deleted; deleted = await collection.DeleteOneAsync(Builders.Filter.Eq("event_id", id)); if (deleted.IsAcknowledged) { Console.WriteLine("----DELETE SUCCESS!----"); Console.WriteLine("Deleted {0} documents",deleted.DeletedCount); } } private static async void DeleteAll(IMongoCollection collection) { long countBefore = collection.CountDocuments(new BsonDocument()); DeleteResult deleted = await collection.DeleteManyAsync(new BsonDocument()); if (deleted.IsAcknowledged) { Console.WriteLine("Deleting all documents in the collection, deleted {0} documents, before:{1} after:{2}\n", deleted.DeletedCount, countBefore, collection.CountDocuments(new BsonDocument())); await collection.Find(new BsonDocument()).ForEachAsync(d => Console.WriteLine(d)); // Display all } } } }