MongoDB Learning Notes ~ Self-encapsulated curd operations (Querying collection object properties, updating collection objects)

Source: Internet
Author: User

Back to Catalog

I have to say that the MONGODB official drive in conjunction with. NET is not very good, not very ideal, so I decided to two times the package, it is very inevitable that everyone wants to use simple objects, but to use complex, troublesome, error-prone objects to do far, it is normal, People are like lazy, like programmers, but also, like lazy, may say, lazy is a sign of the progress of programmers, hehe.

Below I was summed up several standard operations, mainly for the official drive of my package (mongoofficialrepository<tentity>)

1 inserting objects and sub-objects

        /// <summary>        ///adding Objects/// </summary>        Static  Public voidInsert () {List<Person> list =NewList<person>();  for(inti =0; I <Ten; i++)            {                //Add New Objectlist. ADD (NewPerson {Address=NewAddress { City="Beijing", District="Ching", Province="Luanxian",}, Addlist=NewList<address>                {                 NewAddress {Seconds=1, City="Hubei", District="Ching", Province="Luanxian",                 },                  NewAddress {Seconds=1, City="Hunan", District="Community", Province="Luanxian",}}, age= *, Birthday=DateTime.Now, Lastcontact=DateTime.Now, Name="Wangwu"                }); } repository1.        Insert (list); }

2 Update object and sub-object collection elements, this is a very good feature, for no need to update the record, it can not be assigned a value

        /// <summary>        ///Collection Query/// </summary>        Static  Public voidUpdate () {repository1. Update<Person> (i =NewPerson {Id="556bfd1b2683c82060c2edd0", Addlist=NewList<address>                  {                    NewAddress {Id="556bfd1b2683c82060c2edd3", City="Occupied Master 123", District="Ching", Seconds=2                    }                 }              }); }

3 pagination, multi-field query and sorting, this is the most used in project development, wrote a standard for everyone reference

        /// <summary>        ///paging, sorting, querying/// </summary>        Static  Public voidSelect () {//Sorting and retrieving            varM1 = Repository1. Getmodel (New{Address=New{ City="Beijing"}, Addlist=New{Seconds=1                }            }, New{Name = Ordertype.desc},1, -);

4 grouping, for the need to press a number of fields to aggregate (statistics, sums, totals, maximums, minimums, etc.), and multi-criteria query, here are good examples

        /// <summary>        ///Grouping/// </summary>        Static  PublicPagedlist<person> Group (stringKeywordint? Ageintpage) {Specification<Person> spec =NewTruespecification<person>(); //Filter            if(!string. Isnullorwhitespace (keyword)) {spec&=Newdirectspecification<person> (i = I.name = =keyword); }            if(age. HasValue) {Spec&=Newdirectspecification<person> (i = I.age = =Age ); }            //Grouping            varLINQ = fromData1inchRepository1. Getmodel (). Where (Spec. Satisfiedby ()) group Data1 byNew{data1. Id, Data1. Name} into GSelect NewPerson {Id=g.key.id, Name=G.key.name, total=NewTotal {Count=G.count (), Max= G.max (I-=i.age),}}; return NewPagedlist<person> (LINQ, Page,Ten); }

OK, the above is for my mongodb warehousing to carry out some two times the description, feel is still more necessary, hehe.

Back to Catalog

MongoDB Learning Notes ~ Self-encapsulated curd operations (Querying collection object properties, updating collection 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.