/// <summary> /// Demo: Inserting one document /// </summary> /// <param name="collection"></param> /// <returns></returns> private static async Task InsertOne(IMongoCollection <SpetialOffer> collection) { var so = new SpetialOffer { SpecialOfferId = IdToAdd, Description = "Test inserting one", Type = "New Product", Category = "Reseller" }; await collection.InsertOneAsync(so); ConsoleEx.WriteLine("Inserting one document ", ConsoleColor.Red); ConsoleEx.WriteLine("------------------------"); var result = await collection.Find(x => x.SpecialOfferId == IdToAdd).ToListAsync(); // to find in mongo shell execute db.spetialOffer.find({ _id: 20}) // to delete using mongo shell execute db.spetialOffer.remove({_id:20}) foreach (var doc in result) { ConsoleEx.WriteLine($"SpetialOfferId {doc.SpecialOfferId}", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Description {doc.Description}"); ConsoleEx.WriteLine($"Type {doc.Type} ", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Category {doc.Category}"); } }
/// <summary> /// Demo: Upsert /// </summary> /// <param name="collection"></param> /// <returns></returns> private static async Task ReplaceUpsertDemo(IMongoCollection <SpetialOffer> collection) { // if we specify the condition that has no matching document default behavior is to do nothing! var so = new SpetialOffer { SpecialOfferId = IdToUpsert, Description = "Upsert demo", Type = "Upsert type", Category = "Upsert Category" }; await collection.ReplaceOneAsync(x => x.SpecialOfferId == IdToUpsert, so, new UpdateOptions { IsUpsert = true }); ConsoleEx.WriteLine("Update/upsert document ", ConsoleColor.Red); ConsoleEx.WriteLine("------------------------"); var result = await collection.Find(x => x.SpecialOfferId == IdToUpsert).ToListAsync(); // to find in mongo shell execute db.spetialOffer.find({ _id: 20}) // to delete using mongo shell execute db.spetialOffer.remove({_id:20}) ConsoleEx.WriteLine("We are now upserting : " + so.Description, ConsoleColor.Cyan); foreach (var doc in result) { ConsoleEx.WriteLine($"SpetialOfferId {doc.SpecialOfferId}", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Description {doc.Description}", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Type {doc.Type} ", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Category {doc.Category}"); } }
/// <summary> /// Transactions are supported on V4.0 and only for replica sets /// </summary> /// <param name="collection"></param> /// <returns></returns> // ReSharper disable once UnusedMember.Local private static async Task InsertOneWithTransaction(IMongoCollection <SpetialOffer> collection) { var so = new SpetialOffer { SpecialOfferId = IdToAdd, Description = "Test inserting one", Type = "New Product", Category = "Reseller" }; // return IClientSessionHandle object var session = SampleConfig.Client.StartSession(); // start transaction session.StartTransaction(new TransactionOptions( readConcern: ReadConcern.Snapshot, writeConcern: WriteConcern.WMajority)); try { // Note we have to pass session object await collection.InsertOneAsync(session, so); await collection.InsertOneAsync(session, so); // the transaction is commited session.CommitTransaction(); } catch (Exception) { // the transaction is aborted session.AbortTransaction(); } ConsoleEx.WriteLine("Inserting one document ", ConsoleColor.Red); ConsoleEx.WriteLine("------------------------"); var result = await collection.Find(x => x.SpecialOfferId == IdToAdd).ToListAsync(); // to find in mongo shell execute db.spetialOffer.find({ _id: 20}) // to delete using mongo shell execute db.spetialOffer.remove({_id:20}) foreach (var doc in result) { ConsoleEx.WriteLine($"SpetialOfferId {doc.SpecialOfferId}", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Description {doc.Description}"); ConsoleEx.WriteLine($"Type {doc.Type} ", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Category {doc.Category}"); } }
private static SpetialOffer SpetialOfferPoco(DataRow r) { var i = new SpetialOffer { SpecialOfferId = (int)r["SpecialOfferID"], Type = r["Type"].ToString(), Category = r["Category"].ToString(), Description = r["Description"].ToString() }; return(i); }
/// <summary> /// Demo:Replace one document /// </summary> /// <param name="collection"></param> /// <returns></returns> private static async Task ReplaceOneDemo(IMongoCollection <SpetialOffer> collection) { // It is not possible to change ID !!! // if we specify the condition that has no matching document default behavior is to do nothing! var so = new SpetialOffer { SpecialOfferId = IdToAdd, Description = "NEW DESCRIPTION", Type = "NEW TYPE", Category = "NEW Reseller" }; //var uo = new UpdateOptions //{ // IsUpsert = true //}; var resultOfReplace = await collection.ReplaceOneAsync(x => x.SpecialOfferId == IdToAdd, so); ConsoleEx.WriteLine("Replacing one document ", ConsoleColor.Red); ConsoleEx.WriteLine("------------------------"); ConsoleEx.WriteLine($"IsAcknowledged {resultOfReplace.IsAcknowledged}", ConsoleColor.Cyan); ConsoleEx.WriteLine($"IsModifiedCountAvailable {resultOfReplace.IsModifiedCountAvailable}", ConsoleColor.Cyan); ConsoleEx.WriteLine($"MatchedCount {resultOfReplace.MatchedCount}", ConsoleColor.Cyan); ConsoleEx.WriteLine($"UpsertedId {resultOfReplace.UpsertedId}", ConsoleColor.Cyan); ConsoleEx.WriteLine($"ModifiedCount {resultOfReplace.ModifiedCount}", ConsoleColor.Cyan); var result = await collection.Find(x => x.SpecialOfferId == IdToAdd).ToListAsync(); // to find in mongo shell execute db.spetialOffer.find({ _id: 20}) // to delete using mongo shell execute db.spetialOffer.remove({_id:20}) ConsoleEx.WriteLine("Description should be now : " + so.Description, ConsoleColor.Cyan); foreach (var doc in result) { ConsoleEx.WriteLine($"SpetialOfferId {doc.SpecialOfferId}", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Description {doc.Description}", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Type {doc.Type} ", ConsoleColor.Yellow); ConsoleEx.WriteLine($"Category {doc.Category}"); } }