MongoDB Learning Notes ~ Anonymous objects as query parameters, easy to query sub-objects

Source: Internet
Author: User
Tags sorts

For the MongoDB package is still continuing, for the continuous pursuit of simple programming continues, for like the Code of the feeling, still continue ...

When your MONGO data table has sub-objects, especially the list objects, the use of the official driver is very uncomfortable, to remember a lot of new object types, trouble, so, to encapsulate it, let Getmodel support anonymous parameters!

The table structure may be this way

The statement that you want to query becomes this

Look at the above statement feel pretty cool, hehe, look at the implementation of the code below, written this afternoon, hehe!

   PublicIenumerable<tentity> getmodel<u>(U template) {return_table. Find (generatormongoquery (template)). Tolistasync ().        Result; }         PublicPagedresult<tentity> getmodel<u> (intPageIndex,intpageSize) {            return  This. Getmodel (New{}, PageIndex, pageSize); }         PublicPagedresult<tentity> getmodel<u> (U template,intPageIndex,intpageSize) {            return  This. Getmodel (Template,New{}, PageIndex, pageSize); }         PublicPagedresult<tentity> Getmodel<u, o> (U template, O by,intPageIndex,intpageSize) {            #regionConditional filteringbsondocumentfilterdefinition<TEntity> filterdefinition =generatormongoquery (template); #endregion            #regionSorting processingsortdefinition<TEntity> sorts =NewObjectsortdefinition<tentity> (New { }); foreach(varIteminch typeof(O). GetProperties ()) {if((OrderType) item. GetValue ( by) ==ordertype.asc) Sorts=sorts. Ascending (item.                Name); ElseSorts=sorts. Descending (item.            Name); }            #endregion            #regionPagination processingvarSkip = (PageIndex-1) *pageSize; varRecordCount = _table. Find (filterdefinition). Countasync (NewCancellationToken ()).            Result; varLimit =pageSize; return NewPagedresult<tentity>(RecordCount, (int) (RecordCount + pageSize-1) /pageSize, PageSize, PageIndex, _table. Find (filterdefinition). Sort (sorts). Skip (Skip). Limit (limit). Tolistasync ().            Result); #endregion        }

A private method of conditional filtering is proposed, because its logic is used in two methods, so it is extracted

   /// <summary>        ///constructs a query expression for MONGO, via an anonymous object/// </summary>        /// <typeparam name= "U" ></typeparam>        /// <param name= "template" ></param>        /// <returns></returns>        PrivateBsondocumentfilterdefinition<tentity> generatormongoquery<u>(U template) {varQtype =typeof(U); varOutter =Newbsondocument (); varSimpleQuery =Newbsondocument (); foreach(varIteminchqtype.getproperties ()) {                if(item. Propertytype.isclass && Item. PropertyType! =typeof(string))                {                    //complex types, navigation properties, class objects, and collection objects                    foreach(varSubinchitem. Propertytype.getproperties ()) {Simplequery.add (NewBsonelement (item. Name +"."+Sub. Name, Bsonvalue.create (sub. GetValue (item.                    GetValue (template)))); }                }                Else                {                    //simple types, valuetype and stringSimplequery.add (Newbsonelement (item. Name, Bsonvalue.create (item.                GetValue (template))); }            }            return NewBsondocumentfilterdefinition<tentity>(SimpleQuery); }

The result is that we want to be good, how to use it very friendly

MongoDB Learning Notes ~ Anonymous objects as query parameters, easy to query sub-objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.