为了在 C# 中处理多个 MongoDb 过滤器,我编写了以下方法;
public string MultipleFilters(string collectionName, Dictionary<string, string> dictFilters)
{
var filter = Builders<BsonDocument>.Filter.Eq("", "");
foreach (KeyValuePair<string, string> entry in dictFilters)
{
filter = filter & Builders<BsonDocument>.Filter.Eq(entry.Key, entry.Value);
}
var collection = this.database.GetCollection<BsonDocument>(collectionName);
var document = collection.Find(filter).First();
return document.ToJson();
}
但它会抛出错误:
System.InvalidOperationException: 'Sequence contains no elements'