Getting the field name of document without hardcoding field name mongoDB driver c#

image

Example the structure of the document is as above

i wanted to query mongodb to access specific field “VariationList._id” of the document

FilterDefinition<ProductDbm> filterProduct = Builders<ProductDbm>.Filter.In("VariationList._id", wishList.Select(x => x.VariationID)) 

As code above, i have to harcoded a string “VariationList._id” in order to access the field. I have tried using VaraitionList[-1]._id, but it only returns empty data. How can i write without harcoding the field name ? Thanks

It is not clear what you want with:

So you want to query a specific field but you do not want to specify the field name.

It is better if you publish sample documents in text JSON that we can cut-n-paste into our system.

Thanks for your attention

{
  "_id": "62bc1072b95e3868b279af47",
  "CreatedDate": {
    "$date": {
      "$numberLong": "1656492146137"
    }
  },
  "CreatedBy": "62bc1071b95e3868b279af30",
  "LastUpdatedDate": {
    "$date": {
      "$numberLong": "1656492146137"
    }
  },
  "LastUpdatedBy": "62bc1071b95e3868b279af30",
  "Status": "Inactive",
  "ShopID": "62bc1071b95e3868b279af32",
  "Name": "CHARM KASUT BABY ENAMEL",
  "CategoryID": "62bc1071b95e3868b279af41",
  "ProductSKU": "ANIAC_PC039-E1",
  "Description": null,
  "Stock": 3,
  "MinPrice": 388.36,
  "MaxPrice": 391.26,
  "MinPromotionPrice": 0,
  "MaxPromotionPrice": 0,
  "PromotionStartDate": null,
  "PromotionEndDate": null,
  "VariationList": [
    {
      "CreatedDate": {
        "$date": {
          "$numberLong": "1656492146137"
        }
      },
      "CreatedBy": "62bc1071b95e3868b279af30",
      "LastUpdatedDate": {
        "$date": {
          "$numberLong": "1656492146137"
        }
      },
      "LastUpdatedBy": "62bc1071b95e3868b279af30",
      "_id": "62bc1072b95e3868b279af48",
      "Status": "Inactive",
      "VariationSKU": "102102409N",
      "Name": null,
      "Stock": 2,
      "Price": 391.26,
      "PromotionPrice": null,
      "PromotionStartDate": null,
      "PromotionEndDate": null,
      "Attributes": [
        {
          "ID": "62bc1071b95e3868b279af37",
          "Value": "1.22"
        },
        {
          "ID": "62bc1071b95e3868b279af34",
          "Value": "1"
        },
        {
          "ID": "62bc1071b95e3868b279af38",
          "Value": "2.1"
        }
      ],
      "VariationThumbnail": null,
      "VariationImages": null
    },
    {
      "CreatedDate": {
        "$date": {
          "$numberLong": "1656492146137"
        }
      },
      "CreatedBy": "62bc1071b95e3868b279af30",
      "LastUpdatedDate": {
        "$date": {
          "$numberLong": "1656492146137"
        }
      },
      "LastUpdatedBy": "62bc1071b95e3868b279af30",
      "_id": "62bc1072b95e3868b279af49",
      "Status": "Inactive",
      "VariationSKU": "102102412N",
      "Name": null,
      "Stock": 1,
      "Price": 388.36,
      "PromotionPrice": null,
      "PromotionStartDate": null,
      "PromotionEndDate": null,
      "Attributes": [
        {
          "ID": "62bc1071b95e3868b279af37",
          "Value": "1.21"
        },
        {
          "ID": "62bc1071b95e3868b279af34",
          "Value": "1"
        },
        {
          "ID": "62bc1071b95e3868b279af38",
          "Value": "2.1"
        }
      ],
      "VariationThumbnail": null,
      "VariationImages": null
    }
  ],
  "ProductThumbnail": {
    "Primary": null,
    "Secondary": null
  },
  "ProductImageList": [],
  "Tags": {
    "CreatedDate": {
      "$date": {
        "$numberLong": "1656492146137"
      }
    },
    "CreatedBy": "62bc1071b95e3868b279af30",
    "LastUpdatedDate": {
      "$date": {
        "$numberLong": "1656492146137"
      }
    },
    "LastUpdatedBy": "62bc1071b95e3868b279af30",
    "TagtoShow": 0,
    "TagList": [
      "New Product",
      "No Tag",
      "Featured"
    ]
  },
  "Views": 1,
  "SaleScore": 0
}

This is the sample of the document. Back to the question, when specifying the field name in Filter.In it works when I used the string “VariationList._id”. I have an object that maps to the document. When specifying the field name in Filter.In using the object, which I write VaraitionList[-1]._id , it returns empty. How can i specify the field name properly using the object ?

I still do not understand. I am giving up. Hopefully someone with former expertise will kick in. I am curious to see the rest.

My question might be confusing. My bad.

Working code is as below

FilterDefinition<ProductDbm> filterProduct = Builders<ProductDbm>.Filter.In("VariationList._id", wishList.Select(x => x.VariationID)) 

But it fails when I did as below

FilterDefinition<ProductDbm> filterProduct = Builders<ProductDbm>.Filter.In(x => x.VariationList[-1]._id, wishList.Select(x => x.VariationID)) 

I wanted to know why it fails and how to call the filed name correctly.

What you need is C# property name to MongoDB field name conversion.

using MongoDB.Bson.Serialization;
using System.Diagnostics;


var serializer = BsonSerializer.LookupSerializer<YourClass>() as IBsonDocumentSerializer;

// Name of "VariationList"
serializer.TryGetMemberSerializationInfo(nameof(YourClass.VariationList), out var serializationInfo1);
var variationListName = serializationInfo1.ElementName;

// Name of "_id", provided your class has the property with [BsonId]VariationListClass.MyId
serializer.TryGetMemberSerializationInfo(nameof(VariationListClass.MyId), out var serializationInfo2);
var variationListIdName = serializationInfo2.ElementName;

// Output the name in mongodb. You have to concat them into "VariationList._id"
Debug.WriteLine($"{variationListName}.{variationListIdName}");