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,

1 answer

  • answered 2022-05-07 06:53 llwuuzz

    you can use follow code instead

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

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum