Exemplo n.º 1
0
        /// <summary>
        /// 根据实体创建UpdateDefinition
        /// </summary>
        /// <param name="updateEntity"></param>
        /// <param name="isUpsert"></param>
        /// <returns></returns>
        protected UpdateDefinition <TEntity> CreateUpdateDefinition(TEntity updateEntity, bool isUpsert = false)
        {
            long         id    = 0;
            BsonDocument bsDoc = updateEntity.ToBsonDocument();

            bsDoc.Remove(Util.PRIMARY_KEY_NAME);

            UpdateDefinition <TEntity> update = new UpdateDocument("$set", bsDoc);// string.Concat("{$set:", bsDoc.ToJson(), "}");

            //if (isUpsert && isAutoIncrType)
            //{
            //    //如果key不为空或0,则检查是否存在
            //    var exists = updateEntity.ID.Equals(default(TKey)) ? false
            //        : this.Exists(Filter.Eq(Util.PRIMARY_KEY_NAME, updateEntity.ID));

            //    if (!exists)
            //    {
            //        id = CreateIncID();
            //        update = update.SetOnInsert(Util.PRIMARY_KEY_NAME, id);
            //    }
            //}
            if (isUpsert && isAutoIncrType)
            {
                id     = CreateIncID();
                update = update.SetOnInsert(Util.PRIMARY_KEY_NAME, id);
            }

            return(update);
        }
        /// <summary>
        /// 根据实体创建UpdateDefinition
        /// </summary>
        /// <param name="updateEntity"></param>
        /// <param name="isUpsert"></param>
        /// <returns></returns>
        public async Task <UpdateDefinition <TEntity> > CreateUpdateDefinitionAsync(TEntity updateEntity, bool isUpsert = false)
        {
            long         value        = 0L;
            BsonDocument bsonDocument = updateEntity.ToBsonDocument(null, null, default(BsonSerializationArgs));

            bsonDocument.Remove("_id");
            if (isUpsert && updateEntity is IAutoIncr)
            {
                value = await base.CreateIncIDAsync(1L, 0);
            }
            UpdateDefinition <TEntity> updateDefinition = new UpdateDocument("$set", bsonDocument);

            if (isUpsert && updateEntity is IAutoIncr)
            {
                updateDefinition = updateDefinition.SetOnInsert("_id", value);
            }
            return(updateDefinition);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 根据实体创建UpdateDefinition
        /// </summary>
        /// <param name="updateEntity"></param>
        /// <param name="isUpsert"></param>
        /// <returns></returns>
        protected async Task <UpdateDefinition <TEntity> > CreateUpdateDefinitionAsync(TEntity updateEntity, bool isUpsert = false)
        {
            long         id    = 0;
            BsonDocument bsDoc = updateEntity.ToBsonDocument();

            bsDoc.Remove(PoJun.MongoDB.Repository.Util.PRIMARY_KEY_NAME);

            UpdateDefinition <TEntity> update = new UpdateDocument("$set", bsDoc);// string.Concat("{$set:", bsDoc.ToJson(), "}");

            if (isUpsert && isAutoIncrType)
            {
                id = await CreateIncIDAsync();

                update = update.SetOnInsert(PoJun.MongoDB.Repository.Util.PRIMARY_KEY_NAME, id);
            }

            return(update);
        }