Ejemplo n.º 1
0
        public override int Update <TModel>(LambdaQuery.LambdaQuery <TModel> query1, ParameCollection setValue)
        {
            if (query1.__GroupFields.Count > 0)
            {
                throw new CRLException("update不支持group查询");
            }
            if (query1.__Relations.Count > 1)
            {
                throw new CRLException("update关联不支持多次");
            }
            if (setValue.Count == 0)
            {
                throw new ArgumentNullException("更新时发生错误,参数值为空 ParameCollection setValue");
            }
            var query      = query1 as LambdaQuery.MongoDBLambdaQuery <TModel>;
            var collection = _MongoDB.GetCollection <TModel>(query.QueryTableName);
            var update     = Builders <TModel> .Update;
            var first      = setValue.First();
            var updateSet  = update.Set(first.Key, first.Value);

            setValue.Remove(first.Key);
            foreach (var item in setValue)
            {
                if (item.Key.StartsWith("$"))
                {
                    throw new CRLException("MongoDB不支持累加" + item.Key);
                }
                update.Set(item.Key, item.Value);
            }
            var result = collection.UpdateMany(query.__MongoDBFilter, updateSet);

            return((int)result.ModifiedCount);
        }
Ejemplo n.º 2
0
        //public override int Update<TModel, TJoin>(System.Linq.Expressions.Expression<Func<TModel, TJoin, bool>> expression, ParameCollection updateValue)
        //{
        //    throw new NotSupportedException();
        //}
        int Update <TModel>(FilterDefinition <TModel> filter, ParameCollection setValue)
        {
            var collection = GetCollection <TModel>();
            var update     = Builders <TModel> .Update;
            var first      = setValue.First();
            var updateSet  = update.Set(first.Key, first.Value);

            setValue.Remove(first.Key);
            foreach (var item in setValue)
            {
                if (item.Key.StartsWith("$"))
                {
                    throw new CRLException("MongoDB不支持累加" + item.Key);
                }
                updateSet = updateSet.Set(item.Key, item.Value);
            }
            var result = collection.UpdateMany(filter, updateSet);

            return((int)result.ModifiedCount);
        }