C # develop Mongo notes article 3
Today, I tested the query function. Of course, it is mainly used to associate the query with the object we usually use, in addition, the BsonDocument and Collection operations are not very convenient. We first define a user class. Note that this class must exactly match the fields of the objects in mongo, including the case sensitivity, otherwise, a Field Mismatch Error occurs during query. Copy the code public class User {public ObjectId Id {get; set;} public string Name {get; set ;} public int Age {get; set ;}} when the code is copied, the object class is used to associate the query result with the object class. var list = collection. findAllAs <User> (); it is mainly used to find a generic set during findAllAs, but this set cannot be converted to a List set (of course, I won't convert it, but I also hope it will be converted. Brick), it returns a set of repeated curror types, and the operation is not convenient. simply use the weak type to receive it. It is gratifying that this set can be converted to a User object during traversal. The biggest news is that it is too powerful to support the linq Query when querying this set. Let's cheer !! Copy the code Export Database db = export helper. getConnection (); collections collection = db. getCollection <User> ("user"); User user = new User (); user. name = "lisi"; user. age = 20; collection. insert <User> (user); var list = collection. findAllAs <User> (); foreach (User item in list. where (u => u. name = "lisi") {Response. write (item. name);} // Delete // collection. remove (new QueryDocument (); Copying code is simple, mainly testing a query result into an object and a linq condition Query