// Insert a list of documenst in database named "testdb", in a collection named "travel" // update the passenger krishna foodprefrence to only "Indian_NonVeg" public void MongoCrud_04_2_Update_Food_Preference() { #region mongoConnection travelCollection = new MongoClient(_runner.ConnectionString).GetDatabase("testdb").GetCollection <AirTravel>("travel"); #endregion #region data preparation InsertTravelDetails(); #endregion var filter = Builders <AirTravel> .Filter.Eq(x => x.FirstName, "Krishna"); var foodPrefrence = new List <FoodTypes>() { FoodTypes.Indian_NonVeg }; var updateOperation = Builders <AirTravel> .Update.Set(x => x.FoodPreferences, foodPrefrence); travelCollection.UpdateOne(filter, updateOperation); #region verification TravelOperationVerifier.VerifyUpdateFoodPrefrence(_runner.ConnectionString); #endregion #region data cleanup cleanUpTravelCollection(); #endregion }
// Insert a list of documenst in database named "testdb", in a collection named "travel" // upadte the travel date to today UTC of given booking ID "PGS1789" public void MongoCrud_07_2_Find_Customer_with_Booking_ID_AndUpdate_TravelDate() { #region mongoConnection travelCollection = new MongoClient(_runner.ConnectionString).GetDatabase("testdb").GetCollection <AirTravel>("travel"); #endregion #region data preparation InsertTravelDetails(); #endregion var beforeUpdate = travelCollection.Find(Builders <AirTravel> .Filter.Empty).ToList(); var filter = Builders <AirTravel> .Filter.ElemMatch(x => x.TravelHistory, his => his.BookingID == "PGS1789"); var updateOperation = Builders <AirTravel> .Update.Set(x => x.TravelHistory.ElementAt(-1).TravelDate, DateTime.Now); var options = new FindOneAndUpdateOptions <AirTravel> { ReturnDocument = ReturnDocument.After }; var updatedDocuemnt = travelCollection.FindOneAndUpdate(filter, updateOperation, options); #region verification TravelOperationVerifier.Verify_Travel_Date_Updated(_runner.ConnectionString); #endregion #region data cleanup cleanUpTravelCollection(); #endregion }
// Insert a list of documenst in database named "testdb", in a collection named "travel" // add food Prefrence "Indian_Veg" to Shubham public void MongoCrud_07_4_Add_Food_Preference() { #region mongoConnection travelCollection = new MongoClient(_runner.ConnectionString).GetDatabase("testdb").GetCollection <AirTravel>("travel"); #endregion #region data preparation InsertTravelDetails(); var beforeUpdate = travelCollection.Find(Builders <AirTravel> .Filter.Empty).ToList(); #endregion var filter = Builders <AirTravel> .Filter.Eq(x => x.FirstName, "Shubham"); var updateOperation = Builders <AirTravel> .Update.AddToSet(x => x.FoodPreferences, FoodTypes.Indian_Veg); var options = new FindOneAndUpdateOptions <AirTravel> { ReturnDocument = ReturnDocument.After, IsUpsert = true }; var updatedDocuemnt = travelCollection.FindOneAndUpdate(filter, updateOperation, options); #region verification TravelOperationVerifier.Verify_Add_Food_Preference(_runner.ConnectionString); #endregion #region data cleanup cleanUpTravelCollection(); #endregion }
// Insert a list of documenst in database named "testdb", in a collection named "travel" // find the recent second-two travlers with name starting with S order by name desc public void MongoCrud_10_2_Find_Second_two_Recent_Passengers_with_Name_Starting_With_S_Order_By_Name_Dsc() { #region mongoConnection travelCollection = new MongoClient(_runner.ConnectionString).GetDatabase("testdb").GetCollection <AirTravel>("travel"); #endregion #region data preparation InsertTravelDetails(); #endregion int pageSize = 2; int pageNumber = 1; var filter = Builders <AirTravel> .Filter.Regex(x => x.FirstName, BsonRegularExpression.Create(new Regex("S.*"))); var sortDefinition = Builders <AirTravel> .Sort.Descending(x => x.FirstName); var result = travelCollection.Find(filter).Sort(sortDefinition) .Skip(pageSize * pageNumber) .Limit(pageSize).ToList(); #region verification TravelOperationVerifier.Verify_Passengers_NameStarting_with_S(_runner.ConnectionString, result); #endregion #region data cleanup cleanUpTravelCollection(); #endregion }
// find a traveller with booking id "MYS34394" // update the details as present in "FindAndUpdatePassenger" of sirtravel test data // hint : use the jsection reader to read the data. // how to upsert public void MongoCrud_07_5_Update_An_AirTraveller() { #region mongoConnection travelCollection = new MongoClient(_runner.ConnectionString).GetDatabase("testdb").GetCollection <AirTravel>("travel"); #endregion #region data preparation InsertTravelDetails(); #endregion var airTravelPassenger = testData.GetSection("FindAndUpdatePassenger").GetObject <AirTravel>(); var beforeUpdate = travelCollection.Find(Builders <AirTravel> .Filter.Empty).ToList(); var filter = Builders <AirTravel> .Filter.ElemMatch(x => x.TravelHistory, his => his.BookingID == "MYS34394"); var updateOperation = Builders <AirTravel> .Update.Set(x => x, airTravelPassenger); var updatedDocuemnt = travelCollection.UpdateOne(filter, updateOperation, new UpdateOptions { IsUpsert = true }); #region verification TravelOperationVerifier.Verify_Add_Food_Preference(_runner.ConnectionString); #endregion #region data cleanup cleanUpTravelCollection(); #endregion }
// Insert a list of documenst in database named "testdb", in a collection named "travel" // remove all minor passengers public void MongoCrud_06_2_Customers_With_Age_below_18() { #region mongoConnection travelCollection = new MongoClient(_runner.ConnectionString).GetDatabase("testdb").GetCollection <AirTravel>("travel"); #endregion #region data preparation InsertTravelDetails(); #endregion var beforeUpdate = travelCollection.Find(Builders <AirTravel> .Filter.Empty).ToList(); var filter = Builders <AirTravel> .Filter.Lt(x => x.Age, 18); travelCollection.DeleteMany(filter); #region verification TravelOperationVerifier.Verify_No_Minor_Passenger(_runner.ConnectionString); #endregion #region data cleanup cleanUpTravelCollection(); #endregion }