Пример #1
0
        public void MongoDbExtensions_EqualsIgnoreCase_Can_Create_RegEx_Filter_For_Case_Insensitive_Search()
        {
            var collection = _provider.GetDatabase().GetCollection <Well>(ObjectNames.Well141);

            AddParents();

            var filter = Builders <Well> .Filter.Eq(ObjectTypes.Uid, Well.Uid.ToUpper());

            var result = collection.Find(filter).ToList();

            Assert.IsTrue(result.Count == 0);

            var regexFilter = MongoDbExtensions.EqualsIgnoreCase(Well.GetType(), ObjectTypes.Uid, Well.Uid.ToUpper()) as FilterDefinition <Well>;

            result = collection.Find(regexFilter).ToList();
            Assert.IsTrue(result.Count == 1);
            Assert.AreEqual(Well.Name, result[0].Name);

            var regexFilterGeneric = MongoDbExtensions <Well> .EqualsIgnoreCase(ObjectTypes.Uid, Well.Uid.ToUpper());

            result = collection.Find(regexFilterGeneric).ToList();
            Assert.IsTrue(result.Count == 1);
            Assert.AreEqual(Well.Name, result[0].Name);

            var emptyFilter = MongoDbExtensions.EqualsIgnoreCase(Well.GetType(), ObjectTypes.Uid, string.Empty) as FilterDefinition <Well>;

            result = collection.Find(emptyFilter).ToList();
            Assert.IsTrue(result.Count == 0);
        }
Пример #2
0
        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"));
        }
Пример #3
0
        private void PartialDeleteArrayElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, Type type, string parentPath)
        {
            Logger.DebugFormat($"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);
            }
        }