Jing
Jing

Reputation: 31

How to projection fields for a dictionary (C#, MongdoDB)

I am trying my luck here, I have a model which is like the following

public class RowData : BaseBsonDefinition
{
.
  [BsonExtraElements]
  [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
  public Dictionary<string, object> Rows { get; set; } = new(StringComparer.OrdinalIgnoreCase);
.
}

In result, the schema in the MongoDB looks like

{
"_id": {
    "$binary": {
      "base64": "HiuI1sgyT0OZmcgGUit2dw==",
      "subType": "03"
    }
  },
  "c1": "AAA",
  "c8": "Fully Vac",
  "c10": "",
}

Those c1, c8 and c10 fields are keys from the dictionary, my question is how to dynamic project those fields?

I tried

Builders<RowData>.Projection.Exclude(p => "c1")

It seems the MongoDB driver can not handle a value directly.

Anyone could point me in the correct direction?

Thanks,

Upvotes: 1

Views: 319

Answers (1)

llwuuzz
llwuuzz

Reputation: 36

you can use follow code instead

Builders<RowData>.Projection.Exclude("c1");

Upvotes: 1

Related Questions