从MongoDB’集合’获取所有’文档’

我需要检索MongoDB中我的集合中的所有文档,但我无法弄清楚如何。 我已经宣布我的’collections’像这样 –

private static IMongoCollection SpeCollection = db.GetCollection("collection_Project"); 

我按照MongoDB教程中的说明进行了操作。 我根据自己的需要调整了它,比如 –

  var documents = await SpeCollection.Find(new Project()).ToListAsync(); 

但是,我一直有以下错误 –

MongoDB.Driver.IMongoCollection没有’Find’的定义和扩展方法[superlong stuff]的最佳覆盖。 查找包含无效的参数。

使用当前版本的驱动程序(v2.0),您可以通过传递匹配所有内容的filter来实现:

 var documents = await SpeCollection.Find(_ => true).ToListAsync(); 

他们还添加了一个空的filter( FilterDefinition.Empty ),它将到达下一个版本的驱动程序(v2.1):

 var documents = await SpeCollection.Find(Builders.Filter.Empty).ToListAsync(); 

检索所有文件的最简单方法 –

 var documents = SpeCollection.AsQueryable(); 

它也可以转换为Json对象 –

 var json = Json(documents, JsonRequestBehavior.AllowGet); 

如果您想要所有文件,为什么不使用Find all

 var documents = await SpeCollection.Find(new BsonDocument()).ToListAsync();