Example #1
0
        protected bool Exists(OneOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            var findArgs = new FindOneArgs {
                Query          = options.GetMongoQuery(_getIdValue),
                Fields         = Fields.Include(CommonFieldNames.Id),
                ReadPreference = ReadPreference.Primary
            };

            return(_collection.FindOneAs <T>(findArgs) != null);
        }
Example #2
0
        /// <summary>
        /// 查询单条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Field">查询属性字段</param>
        /// <param name="Value">字段值</param>
        /// <returns>返回当前实体</returns>
        public T FindOne <T>(string Field, string Value)
        {
            T oneEntity = default(T);

            try
            {
                FindOneArgs args = new FindOneArgs
                {
                    Query = Query.EQ(Field, Value)
                };
                oneEntity = this._db.GetCollection(typeof(T).Name).FindOneAs <T>(args);
                this._db.GetCollection(typeof(T).Name).FindAs <T>(Query.GTE(Field, Value)).ToList();
            }
            catch (Exception ex)
            {
            }
            return(oneEntity);
        }
Example #3
0
        protected TModel FindOne <TModel>(OneOptions options) where TModel : class, new()
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            TModel result = null;

            if (options.UseCache)
            {
                result = Cache.Get <TModel>(GetScopedCacheKey(options.CacheKey));
            }

            if (result != null)
            {
                return(result);
            }

            var findArgs = new FindOneArgs {
                Query = options.GetMongoQuery(_getIdValue), Fields = Fields.Include(options.Fields.ToArray())
            };

            var mongoOptions = options as MongoOptions;

            if (mongoOptions != null && mongoOptions.SortBy != null)
            {
                findArgs.SortBy = mongoOptions.SortBy;
            }

            if (mongoOptions != null && mongoOptions.ReadPreference != null)
            {
                findArgs.ReadPreference = mongoOptions.ReadPreference;
            }

            result = _collection.FindOneAs <TModel>(findArgs);
            if (result != null && options.UseCache)
            {
                Cache.Set(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate());
            }

            return(result);
        }
Example #4
0
        public virtual TResource GetResource(string id, params Expression <Func <TResource, object> >[] explicitPropertyList)
        {
            var mongoQuery = Query.And(
                Query.NotExists("_deleted"),
                Query <TResource> .EQ(r => r.Id, id));

            var args = new FindOneArgs
            {
                Query  = mongoQuery,
                Fields = Fields <TResource> .Include(explicitPropertyList)
            };

            if (explicitPropertyList.Length > 0)
            {
                //need to add the type discriminator
                args.Fields.ToBsonDocument().Add("_t", 1);
            }

            return(ResourceCollection.FindOneAs <TResource>(args));
        }