在.NET驱动程序中以`IMongoCollection <t>`的序列检索MongoDB集合

时间:2019-03-12 14:11:34

标签: mongodb mongodb-.net-driver

IMongoDatabase.ListCollections将光标移到BsonDocument上。 为什么它不返回光标到IMongoCollection<T>上?

我试图编写一个通用的GetCollection方法来检索仅给出文档类型的集合,就像这样:

private IMongoCollection<T> GetCollection<T>()
{
    var client = new MongoClient("connectionString");
    var db = client.GetDatabase("dbName");
    var coll = db.ListCollectionsAsync().Result.ToListAsync().Result
        // Find collection of document of type T
        // Collection is a BsonDocument instead
        .Find(collection => typeof(collection) == typeof(T));

    return coll;
}

0 个答案:

没有答案