C # Development MONGO notes, article Three

Source: Internet
Author: User

Today, the main test of the query function, of course, is mainly to let the query and we usually use the entity object associated with, and those bsondocument and collection we operate is not too convenient

Or first define a user class, notice that this class needs to match the field of the object in the MONGO exactly, including the case, otherwise the field cannot match the error when the query occurs

     Public class User    {        publicgetset;}          Public string Get Set ; }           Public int Get Set ; }          }

Having an entity class is how the query result is associated with the entity class.

Var list= collection. Findallas<user> ();

Mainly in the time of Findallas to find a generic collection, but this collection can not be turned into a list collection (of course, I will not turn, but also want to turn the shot brick), it returns the collection is Mongocurror type, operation is not convenient, simply use the weak type to receive it.

It is gratifying to note that this collection can be converted to a user object when traversing.

The great good news is that the LINQ query is supported when querying this set, isn't it too powerful, let's cheer together!!

Mongodatabase db =mongohelper.getconnection (); Mongocollection Collection= db. Getcollection<user> ("User"); User User=NewUser (); User. Name="Lisi"; User. Age= -; Collection. Insert<User>(user); varList= collection. Findallas<user>(); foreach(User IteminchList. Where (u=>u.name=="Lisi") {Response.Write (item).       Name); }            //Delete//collection. Remove (New Querydocument ());

The code is simple, mostly testing a query that turns the query result into an object and a LINQ condition

C # Development MONGO notes, article Three

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.