/// <summary> /// Deletes a data object by the specified identifier. /// </summary> /// <typeparam name="TObject">The type of data object.</typeparam> /// <param name="uri">The data object URI.</param> /// <param name="dbCollectionName">The name of the database collection.</param> /// <exception cref="WitsmlException"></exception> protected void DeleteEntity <TObject>(EtpUri uri, string dbCollectionName) { try { Logger.DebugFormat("Deleting from {0} MongoDb collection", dbCollectionName); var collection = GetCollection <TObject>(dbCollectionName); var current = GetEntity <TObject>(uri, dbCollectionName); if (current == null) { return; } var transaction = Transaction; if (transaction != null) { //var document = MongoDbUtility.GetDocumentId(current); transaction.Attach(MongoDbAction.Delete, dbCollectionName, IdPropertyName, null, uri); transaction.Save(); } else { var filter = MongoDbUtility.GetEntityFilter <TObject>(uri, IdPropertyName); collection.DeleteOne(filter); } } catch (MongoException ex) { Logger.ErrorFormat("Error deleting from {0} MongoDb collection: {1}", dbCollectionName, ex); throw new WitsmlException(ErrorCodes.ErrorDeletingFromDataStore, ex); } }
public void MongoDbUtility_GetEntityFilter_Can_Get_Id_Filter_From_Uri() { var collection = _provider.GetDatabase().GetCollection <Wellbore>(ObjectNames.Wellbore141); var wellbore141Uri = "eml://witsml14/well(well141)/wellbore(wellbore141)"; var filters141 = MongoDbUtility.GetEntityFilter <Wellbore>(new EtpUri(wellbore141Uri)); Assert.IsNotNull(filters141); var filters141Json = filters141.Render(collection.DocumentSerializer, collection.Settings.SerializerRegistry); Assert.IsNotNull(filters141Json); Assert.AreEqual(2, filters141Json.ElementCount); var filterElements = filters141Json.Elements.ToList(); Assert.AreEqual(ObjectTypes.Uid, filterElements[0].Name); Assert.AreEqual("UidWell", filterElements[1].Name); var wellbore200Uri = "eml://witsml20/wellbore(wellbore200)"; var filters200 = MongoDbUtility.GetEntityFilter <Wellbore>(new EtpUri(wellbore200Uri), ObjectTypes.Uuid); var filters200Json = filters200.Render(collection.DocumentSerializer, collection.Settings.SerializerRegistry); Assert.IsNotNull(filters200Json); Assert.AreEqual(1, filters200Json.ElementCount); Assert.AreEqual(ObjectTypes.Uuid, filters200Json.Elements.ToList()[0].Name); }
/// <summary> /// Updates the specified entity. /// </summary> /// <param name="entity">The entity.</param> /// <param name="uri">The URI.</param> /// <param name="updates">The updates.</param> public void Update(T entity, EtpUri uri, Dictionary <string, object> updates) { Logger.DebugFormat($"Updating data object: {uri}"); _entityFilter = MongoDbUtility.GetEntityFilter <T>(uri, IdPropertyName); Entity = entity; Context.Update = Update(updates, uri.ObjectId); BuildUpdate(Parser.Element()); LogUpdateFilter(_entityFilter, Context.Update); Collection.UpdateOne(_entityFilter, Context.Update); WitsmlOperationContext.Current.Warnings.AddRange(Context.Warnings); }
/// <summary> /// Executes partial delete. /// </summary> /// <param name="entity">The entity.</param> /// <param name="uri">The URI.</param> /// <param name="updates">The updates.</param> public void PartialDelete(T entity, EtpUri uri, Dictionary <string, object> updates) { Logger.DebugFormat($"Partial deleting data object: {uri}"); _entityFilter = MongoDbUtility.GetEntityFilter <T>(uri, _idPropertyName); _entity = entity; Context.Update = Update(updates, uri.ObjectId); BuildPartialDelete(_parser.Element()); LogUpdateFilter(_entityFilter, Context.Update); _collection.UpdateOne(_entityFilter, Context.Update); // Remove recurring elements after all update because of the position being used in field path RemoveArrayElementsByDepth(); WitsmlOperationContext.Current.Warnings.AddRange(Context.Warnings); }
/// <summary> /// Gets the entities having the supplied URIs found in the specified collection. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="uris">The uris.</param> /// <param name="dbCollectionName">Name of the database collection.</param> /// <returns>The query results.</returns> protected List <TObject> GetEntities <TObject>(IEnumerable <EtpUri> uris, string dbCollectionName) { var list = uris.ToList(); Logger.DebugFormat("Querying {0} MongoDb collection by URIs: {1}{2}", dbCollectionName, Environment.NewLine, Logger.IsDebugEnabled ? string.Join(Environment.NewLine, list) : null); if (!list.Any()) { return(GetCollection <TObject>(dbCollectionName) .Find("{}") .ToList()); } var filters = list.Select(x => MongoDbUtility.GetEntityFilter <TObject>(x, IdPropertyName)); return(GetCollection <TObject>(dbCollectionName) .Find(Builders <TObject> .Filter.Or(filters)) .ToList()); }
/// <summary> /// Gets the entity filter for the specified URI. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="uri">The URI.</param> /// <param name="idPropertyName">Name of the identifier property.</param> /// <returns>The entity filter.</returns> protected virtual FilterDefinition <TObject> GetEntityFilter <TObject>(EtpUri uri, string idPropertyName) { return(MongoDbUtility.GetEntityFilter <TObject>(uri, idPropertyName)); }