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)