How to convert mongo document into key value pair in .Net?

Published on Author Code Father

There is a ToDictionary method that can map this.

var document = collection.Find(filter).Project(projection);
var dictionary = documents.ToEnumerable().ToDictionary(key => key.client, value => value.url);

A different approach

var docs = new Dictionary<string, BsonDocument>();
var result = collection.Find(filter).Project(projection);      
result.ForEachAsync((bsonDoc) =>
    string name = bsonDoc.GetValue("[Your Dictionary Key]").AsString;
    if (!docs.ContainsKey(name))
        docs[name] = bsonDoc;}});