// 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 }
// 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 }