Search code examples
c#mongodbmongodb-.net-drivermongodb-csharp-2.0

Unable to to query Dictionary item using MongoDB C# 2.0 Drivers


I have a class that has a dictionary property in it.

    [DataMember]
    [BsonElement("QueriableParameters")]
    public Dictionary<string, string> QueriableParameters
    {
        get;
        set;
    }

I'm using the new MongoDB c# 2.0 drivers and can't seem to be able to do this:

var selectQuery1 = await collection.Find(s => s.QueriableParameters["UniqueLoanNumber"] == "3049793b-91eb-49d8-a5b4-7cbfd1a1bb3c").ToListAsync();

I get this error stating that:

InnerException: System.InvalidOperationException
   HResult=-2146233079
   Message=s.QueriableParameters.get_Item("UniqueLoanNumber") is not supported.
   Source=MongoDB.Driver
   StackTrace:
        at MongoDB.Driver.Linq.Translators.PredicateTranslator.GetSerializationInfo(Expression expression)
        at MongoDB.Driver.Linq.Translators.PredicateTranslator.BuildComparisonQuery(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)

Please help point me in the right direction.

Thanks,


Solution

  • Thank you Craig for the information.

    I guess the only way to do it right now is to :

    var builders = Builders<NotificationData>.Filter;
    var filter = builders.Eq("QueriableParameters.UniqueLoanNumber", "theIdLookingfor");
    var selectQuery = await collection.Find(filter).ToListAsync();