public void MongoDbExtensions_PullFilter_Can_Create_Pull_Filter_Update_Definition_Expression()
        {
            var collection = _provider.GetDatabase().GetCollection <Log>(ObjectNames.Log141);

            AddParents();
            DevKit.InitHeader(Log, LogIndexType.measureddepth);
            DevKit.AddAndAssert <LogList, Log>(Log);

            var addedLog = DevKit.GetAndAssert(Log);

            Assert.AreEqual(1, addedLog.LogCurveInfo.Count(l => l.Mnemonic.Value == "ROP"));
            var childFilter = Builders <LogCurveInfo> .Filter.Eq("Mnemonic.Value", "ROP");

            var updateDefinition = MongoDbExtensions.PullFilter(typeof(Log), typeof(LogCurveInfo), ObjectTypes.LogCurveInfo.ToPascalCase(), childFilter) as UpdateDefinition <Log>;
            var log = collection.Find(Builders <Log> .Filter.Eq(ObjectTypes.Uid, Log.Uid));

            Assert.IsNotNull(log);
            collection.UpdateOne(Builders <Log> .Filter.Eq(ObjectTypes.Uid, Log.Uid), updateDefinition);
            var updatedLog = DevKit.GetAndAssert(Log);

            Assert.AreEqual(0, updatedLog.LogCurveInfo.Count(l => l.Mnemonic.Value == "ROP"));

            Assert.AreEqual(1, updatedLog.LogCurveInfo.Count(l => l.Uid == "GR"));
            var childFilterGr = Builders <LogCurveInfo> .Filter.Eq(ObjectTypes.Uid, "GR");

            var updateDefinitionGeneric = MongoDbExtensions <Log, LogCurveInfo> .PullFilter(ObjectTypes.LogCurveInfo.ToPascalCase(), childFilterGr);

            collection.UpdateOne(Builders <Log> .Filter.Eq(ObjectTypes.Uid, Log.Uid), updateDefinitionGeneric);
            updatedLog = DevKit.GetAndAssert(Log);
            Assert.AreEqual(0, updatedLog.LogCurveInfo.Count(l => l.Uid == "GR"));
        }
Beispiel #2
0
        private void PartialDeleteArrayElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, Type type, string parentPath)
        {
            Logger.Debug($"Partial deleting array elements: {parentPath} {propertyInfo?.Name}");

            var updateBuilder = Builders <T> .Update;
            var filterBuilder = Builders <T> .Filter;
            var idField       = MongoDbUtility.LookUpIdField(type);
            var filterPath    = GetPropertyPath(parentPath, idField);
            var properties    = GetPropertyInfo(type);

            var ids       = new List <string>();
            var itemsById = GetItemsById((IEnumerable)propertyValue, properties, idField, ids);

            var updateList = elements
                             .Select(element =>
            {
                var elementId = GetElementId(element, idField);
                if (string.IsNullOrEmpty(elementId) || propertyInfo == null)
                {
                    return(null);
                }

                var filters = new List <FilterDefinition <T> >()
                {
                    _entityFilter
                };

                object current;
                itemsById.TryGetValue(elementId, out current);

                if (current == null)
                {
                    return(null);
                }

                var elementFilter = Builders <T> .Filter.EqIgnoreCase(filterPath, elementId);
                filters.Add(elementFilter);
                var filter = filterBuilder.And(filters);

                if (element.Elements().Any())
                {
                    var position     = ids.IndexOf(elementId);
                    var positionPath = parentPath + "." + position;

                    var update = updateBuilder.Set(GetPropertyPath(positionPath, idField), elementId);

                    var saveUpdate = Context.Update;
                    Context.Update = update;

                    PushPropertyInfo(propertyInfo, current);
                    NavigateElement(element, type, positionPath);
                    PopPropertyInfo();

                    var model      = new UpdateOneModel <T>(filter, Context.Update);
                    Context.Update = saveUpdate;
                    return(model);
                }
                else
                {
                    if (IsRequired(propertyInfo) && itemsById.Count == 1)
                    {
                        throw new WitsmlException(ErrorCodes.MustRetainOneRecurringNode);
                    }

                    var childFilter = MongoDbExtensions.EqualsIgnoreCase(type, idField, elementId);
                    var update      = MongoDbExtensions.PullFilter(typeof(T), type, parentPath, childFilter) as UpdateDefinition <T>;

                    if (childFilter != null && update != null)
                    {
                        //var update = updateBuilder.Pull(parentPath, current);
                        AddToPullCollection(parentPath, new UpdateOneModel <T>(filter, update));
                    }

                    return(null);
                }
            })
                             .Where(x => x != null)
                             .ToList();

            if (updateList.Count > 0)
            {
                _collection.BulkWrite(updateList);
            }
        }