Using SetFields with MongoDB C # 2.0 Driver

Using the old driver, I can specify the fields that I would like to return from the request, as follows:

var cursor = Collection.Find(query).
  SetFields(Fields<MealPlan>.Exclude (plan => plan.Meals));

How to do this with driver 2.0?

+4
source share
1 answer

You need to use a method Projectionon IFindFluent(which returns Findand Projection):

var findFluent = Collection.Find(query).Projection(Fields<MealPlan>.Exclude (plan => plan.Meals))

Now this will lead to the creation of a cursor BsonDocumentbecause it does not know what the projection looks like. You can call generic Projectioninstead to add this type:

var findFluent = Collection.Find(query).Projection<MealPlan>(Fields<MealPlan>.Exclude (plan => plan.Meals))

( Exclude), , :

var findFluent = Collection.Find(query).Projection(plan => plan.Meals)
+4

All Articles