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