MongoDB學習筆記~以匿名對象做為查詢參數,方便查詢子物件

來源:互聯網
上載者:User

標籤:

對於MongoDB的封裝還在繼續,對於不斷追求簡單的編程還在繼續,對於喜歡代碼的那麼感覺,還在繼續...

當你的mongo資料表裡有子物件時,尤其是列表對象時,使用官方的驅動很是不爽,要記得很多新的物件類型,麻煩,所以,將它進行封裝,讓GetModel支援匿名參數!

表結構可能是這樣

希望查詢的語句變成這樣

看了上面的語句感覺挺酷吧,呵呵,下面看一下實現的代碼,今天下午寫的,呵呵!

  public IEnumerable<TEntity> GetModel<U>(U template)        {            return _table.Find(GeneratorMongoQuery(template)).ToListAsync().Result;        }        public PagedResult<TEntity> GetModel<U>(int pageIndex, int pageSize)        {            return this.GetModel(new { }, pageIndex, pageSize);        }        public PagedResult<TEntity> GetModel<U>(U template, int pageIndex, int pageSize)        {            return this.GetModel(template, new { }, pageIndex, pageSize);        }        public PagedResult<TEntity> GetModel<U, O>(U template, O orderby, int pageIndex, int pageSize)        {            #region 條件過濾            BsonDocumentFilterDefinition<TEntity> filterDefinition = GeneratorMongoQuery(template);            #endregion            #region 排序處理            SortDefinition<TEntity> sorts = new ObjectSortDefinition<TEntity>(new { });            foreach (var item in typeof(O).GetProperties())            {                if ((OrderType)item.GetValue(orderby) == OrderType.Asc)                    sorts = sorts.Ascending(item.Name);                else                    sorts = sorts.Descending(item.Name);            }            #endregion            #region 分頁處理            var skip = (pageIndex - 1) * pageSize;            var recordCount = _table.Find(filterDefinition).CountAsync(new CancellationToken()).Result;            var limit = pageSize;            return new PagedResult<TEntity>(                recordCount,                (int)(recordCount + pageSize - 1) / pageSize,                pageSize,                pageIndex,                _table.Find(filterDefinition)                      .Sort(sorts)                      .Skip(skip)                      .Limit(limit)                      .ToListAsync().Result);            #endregion        }

提出了一個條件過濾的私人方法,因為它的邏輯在兩個方法裡都用了,所以進行提取

   /// <summary>        /// 構建Mongo的查詢運算式,通過一個匿名對象        /// </summary>        /// <typeparam name="U"></typeparam>        /// <param name="template"></param>        /// <returns></returns>        private BsonDocumentFilterDefinition<TEntity> GeneratorMongoQuery<U>(U template)        {            var qType = typeof(U);            var outter = new BsonDocument();            var simpleQuery = new BsonDocument();            foreach (var item in qType.GetProperties())            {                if (item.PropertyType.IsClass && item.PropertyType != typeof(string))                {                    //複雜類型,導覽屬性,類對象和集合對象                     foreach (var sub in item.PropertyType.GetProperties())                    {                        simpleQuery.Add(new BsonElement(item.Name + "." + sub.Name,                           BsonValue.Create(sub.GetValue(item.GetValue(template)))                            ));                    }                }                else                {                    //簡單類型,ValueType和string                    simpleQuery.Add(new BsonElement(item.Name,                      BsonValue.Create(item.GetValue(template))                        ));                }            }            return new BsonDocumentFilterDefinition<TEntity>(simpleQuery);        }

結果就是我們想好的,怎麼樣,用法挺友好吧

MongoDB學習筆記~以匿名對象做為查詢參數,方便查詢子物件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.