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


i have document type attributes, 1 of dictionary< string, string >.

serialization , de-serealization seem work fine (i can search, perform crud operations, etc.). problem i'm trying write method find objects of type dictionary contains specific value (id) among 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 exception while building query:

any requires serializer specified dictionary support items implementing  mongodb.bson.serialization.ibsonarrayserializer , returning non-null result.  mongodb.bson.serialization.serializers.dictionaryserializer`2[system.string,system.string]  current serializer. 

i suspect problem related fact c# driver's default serializer doesn't support this. there workaround?

i've tried perform "contains" id string on dictionary.keys collection, gives notsupportedexception: unable determine serialization information expression: m.dictionary.keys.

figured out on own...

added annotation in myclass specify how want dictionary serialized (read dictionaryserializationoptions here more info).

[bsonelement("dictionary")] [bsondictionaryoptions(dictionaryrepresentation.arrayofdocuments)] // added this! public dictionary<string, string> dictionary { get; set; } 

this serializes dictionary array of documents, each containing "k" (key) , "v" (value).

then changed query to:

var query = query.elemmatch("dictionary", query.eq("k", id)); 

this searches entries in collection in dictionary contains id 1 of keys.

other relevant links:


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -