简体   繁体   中英

MongoDB c# Driver - Perform a LINQ “Any” in a Serialized Dictionary

I have a document type with some attributes, one of which is a Dictionary< string, string >.

Serialization and De-serealization seem to work fine (I can search, perform CRUD operations, etc.). The problem is that I'm trying to write a method to find all the objects of that type where the dictionary contains a specific value (id) among its Keys.

Attempted Query:

var objectCollection = db.GetCollection<MyClass>("MyClass");

var query = Query<MyClass>.Where(m => m.MyDictionary.Any(k => k.Key == id));

The Class:

public class MyClass
{

    public ObjectId Id { get; set; }
    // ...
    [BsonElement("Dictionary")]
    public Dictionary<string, string> MyDictionary { get; set; }
}

...but I get this Exception while building the query:

Any requires that the serializer specified for Dictionary support items by implementing 
MongoDB.Bson.Serialization.IBsonArraySerializer and returning a non-null result. 
MongoDB.Bson.Serialization.Serializers.DictionarySerializer`2[System.String,System.String] 
is the current serializer.

I suspect the problem is related to the fact that the c# driver's default serializer doesn't support this. Is there a workaround?

I've also tried to perform a "Contains" for the id string on the Dictionary.Keys collection, but that gives a NotSupportedException: Unable to determine the serialization information for the expression: m.Dictionary.Keys.

Figured it out on my own...

Added an annotation in MyClass to specify how I want the Dictionary to be serialized ( read about DictionarySerializationOptions here for more info).

[BsonElement("Dictionary")]
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)] // Added This!
public Dictionary<string, string> Dictionary { get; set; }

This serializes the Dictionary as an array of Documents, each containing a "k" (key) and "v" (value).

Then I changed my query to:

var query = Query.ElemMatch("Dictionary", Query.EQ("k", id));

This searches entries in my collection in which the Dictionary contains id as one of its keys.

Other relevant links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM