You use .AsQueryable() from System.Linq , because of this, it filters everything on the client side. But you need to use .AsQueryable() from MongoCollection to filter the data in the database. This extension method creates a MongoQueryable<T> .
I believe the following should work:
//or you could use your projection class instead of BsonDocument var query = Items.AsQueryable<BsonDocument>() .Select(x=> new {id = x["_id"].AsObjectId, Name=x["Name"].AsString}); var query1 = query.Where(d=>d.Name == "Ken").ToList();
Update:
If you want to use SetFields , you need to use the old query syntax:
_col.Find(Query<UserDocument>.EQ(x=> x.Name, "Ken")).SetFields(..).
You can also use SetFields without magic lines as follows:
cursor.SetFields(Fields<UserDocument>.Include(x=> x.Name, x=> x.Age))
Using linq, SetFields is done through Select .
Hope this helps.
source share