//shared methods
 protected virtual void UpdateField(Updates <T> updates, T entity, bool wasInserted)
 {
     if (updates.Sets != null)
     {
         foreach (Update <T> update in updates.Sets)
         {
             PropertyInfo propertyInfo = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             propertyInfo.SetValue(entity, update.Value);
         }
     }
     if (updates.Increments != null)
     {
         foreach (Update <T> update in updates.Increments)
         {
             PropertyInfo propertyInfo  = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             int          propertyValue = (int)propertyInfo.GetValue(entity);
             propertyValue += (int)update.Value;
             propertyInfo.SetValue(entity, propertyValue);
         }
     }
     if (updates.SetOnInserts != null && wasInserted)
     {
         foreach (Update <T> update in updates.SetOnInserts)
         {
             PropertyInfo propertyInfo = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             propertyInfo.SetValue(entity, update.Value);
         }
     }
     if (updates.Pushes != null)
     {
         foreach (UpdateCollection <T> update in updates.Pushes)
         {
             PropertyInfo propertyInfo  = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             IEnumerable  propertyValue = (IEnumerable)propertyInfo.GetValue(entity);
             ExpressionReflection.AddToEnumerable(propertyValue, update.Value);
         }
     }
     if (updates.Pulls != null)
     {
         foreach (UpdateCollection <T> update in updates.Pulls)
         {
             PropertyInfo propertyInfo  = ExpressionReflection.GetPropertyInfo(update.PropertyExpression.Body);
             IEnumerable  propertyValue = (IEnumerable)propertyInfo.GetValue(entity);
             ExpressionReflection.RemoveFromEnumerable(propertyValue, update.Value);
         }
     }
 }
        protected virtual UpdateDefinition <T> ToUpdateDefinition(Updates <T> updates)
        {
            UpdateDefinition <T> updateDefinition = Builders <T> .Update.Combine();

            if (updates.Sets != null)
            {
                foreach (Update <T> update in updates.Sets)
                {
                    updateDefinition = updateDefinition.Set(update.PropertyExpression, update.Value);
                }
            }
            if (updates.Increments != null)
            {
                foreach (Update <T> update in updates.Increments)
                {
                    updateDefinition = updateDefinition.Inc(update.PropertyExpression, update.Value);
                }
            }
            if (updates.SetOnInserts != null)
            {
                foreach (Update <T> update in updates.SetOnInserts)
                {
                    updateDefinition = updateDefinition.SetOnInsert(update.PropertyExpression, update.Value);
                }
            }
            if (updates.Pushes != null)
            {
                foreach (UpdateCollection <T> update in updates.Pushes)
                {
                    var property = new ExpressionFieldDefinition <T>(update.PropertyExpression);
                    updateDefinition = updateDefinition.Push(property, update.Value);
                }
            }
            if (updates.Pulls != null)
            {
                foreach (UpdateCollection <T> update in updates.Pulls)
                {
                    var property = new ExpressionFieldDefinition <T>(update.PropertyExpression);
                    updateDefinition = updateDefinition.Pull(property, update.Value);
                }
            }

            return(updateDefinition);
        }
        public virtual Task <long> UpdateMany(Expression <Func <T, bool> > filterConditions, Updates <T> updates, CancellationToken token = default)
        {
            List <T> entitiesToUpdate = Collection.Where(x => filterConditions.Compile().Invoke(x)).ToList();

            foreach (T entity in entitiesToUpdate)
            {
                UpdateField(updates, entity, false);
            }
            return(Task.FromResult <long>(entitiesToUpdate.Count));
        }
        public virtual Task <long> UpdateOne(Expression <Func <T, bool> > filterConditions, Updates <T> updates, CancellationToken token = default)
        {
            T entity = Collection.Where(x => filterConditions.Compile().Invoke(x)).FirstOrDefault();

            if (entity != null)
            {
                UpdateField(updates, entity, false);
            }
            long updatedCount = entity == null ? 0 : 1;

            return(Task.FromResult <long>(updatedCount));
        }
        public virtual Task <T> FindOneAndUpdate(Expression <Func <T, bool> > filterConditions, Updates <T> updates,
                                                 ReturnDocument returnDocument = ReturnDocument.Before, CancellationToken token = default)
        {
            T entity = Collection.Where(x => filterConditions.Compile().Invoke(x)).FirstOrDefault();

            T returnedEntity = entity;

            if (returnDocument == ReturnDocument.Before)
            {
                //Could clone it here if required with AutoMapper or BinarySerializer.
                //However BinarySerializer does not serialize ObjectId.
                returnedEntity = entity;
            }

            UpdateField(updates, entity, false);
            return(Task.FromResult(returnedEntity));
        }
예제 #6
0
        //ctor
        public static Updates <T> Empty()
        {
            var updates = new Updates <T>();

            return(updates);
        }
        public virtual async Task <long> UpdateMany(Expression <Func <T, bool> > filterConditions, Updates <T> updates, CancellationToken token = default)
        {
            FilterDefinition <T> filter           = ToFilter(filterConditions);
            UpdateDefinition <T> updateDefinition = ToUpdateDefinition(updates);

            UpdateResult result = await _collection
                                  .UpdateManyAsync(filter, updateDefinition, cancellationToken : token)
                                  .ConfigureAwait(false);

            return(result.ModifiedCount);
        }
        public virtual async Task <long> UpdateOne(Expression <Func <T, bool> > filterConditions, Updates <T> updates, CancellationToken token = default)
        {
            var filter = Builders <T> .Filter.Where(filterConditions);

            UpdateDefinition <T> update = ToUpdateDefinition(updates);
            UpdateResult         result = await _collection.UpdateOneAsync(filter, update, cancellationToken : token)
                                          .ConfigureAwait(false);

            return(result.ModifiedCount);
        }
        public virtual Task <T> FindOneAndUpdate(Expression <Func <T, bool> > filterConditions, Updates <T> updates,
                                                 ReturnDocument returnDocument = ReturnDocument.Before, CancellationToken token = default)
        {
            FilterDefinition <T> filter           = ToFilter(filterConditions);
            UpdateDefinition <T> updateDefinition = ToUpdateDefinition(updates);
            var options = new FindOneAndUpdateOptions <T>
            {
                ReturnDocument = returnDocument
            };

            return(_collection.FindOneAndUpdateAsync(filter, updateDefinition, options, cancellationToken: token));
        }