43

我需要在 MongoDB 中检索我的集合中的所有文档,但我不知道如何。我已经像这样宣布了我的“收藏”-

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

我遵循了这个MongoDB 教程中的解释。我根据自己的需要调整了它,比如——

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

但是,我一直遇到以下错误-

MongoDB.Driver.IMongoCollection 没有“查找”的定义和扩展方法的最佳覆盖[超长的东西]。查找包含无效参数。

4

3 回答 3

99

使用当前版本的驱动程序(v2.0),您可以通过传递匹配所有内容的过滤器来做到这一点:

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

他们还添加了一个空过滤器 ( FilterDefinition.Empty),它将在下一个版本的驱动程序 (v2.1) 中出现:

var documents = await SpeCollection.Find(Builders<Project>.Filter.Empty).ToListAsync();
于 2015-05-26T11:37:52.160 回答
17

最简单的方法

检索所有文件-

var documents = SpeCollection.AsQueryable();

也转换为JSON对象-

var json = Json(documents, JsonRequestBehavior.AllowGet);
于 2016-02-07T09:37:51.027 回答
7

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

var documents = await SpeCollection.Find(new BsonDocument()).ToListAsync();
于 2015-05-26T08:55:38.743 回答