从 MongoDB 'collection' 获取全部 'documents'
Get All 'documents' from MongoDB 'collection'
我需要在 MongoDB 中检索我的 collection 中的所有文档,但我不知道如何做。我已经这样声明我的 'collection'-
private static IMongoCollection<Project> SpeCollection = db.GetCollection<Project>("collection_Project");
然后我按照 this MongoDB 教程中的说明进行了操作。我根据自己的需要进行了调整,比如-
var documents = await SpeCollection.Find(new Project()).ToListAsync();
但是,我一直有以下错误-
MongoDB.Driver.IMongoCollection does not have a definition for 'Find' and the best override of the extension method [superlong stuff]. Find contains non valid arguments.
如果你想要所有文件,为什么不使用Find all
?
var documents = await SpeCollection.Find(new BsonDocument()).ToListAsync();
使用当前版本的驱动程序 (v2.0),您可以通过传递匹配所有内容的过滤器来做到这一点:
var documents = await SpeCollection.Find(_ => true).ToListAsync();
他们还添加了一个空过滤器 (FilterDefinition.Empty
),它将出现在下一版本的驱动程序 (v2.1) 中:
var documents = await SpeCollection.Find(Builders<Project>.Filter.Empty).ToListAsync();
最简单的方法
检索所有文档-
var documents = SpeCollection.AsQueryable();
也转换为JSON
对象-
var json = Json(documents, JsonRequestBehavior.AllowGet);
我需要在 MongoDB 中检索我的 collection 中的所有文档,但我不知道如何做。我已经这样声明我的 'collection'-
private static IMongoCollection<Project> SpeCollection = db.GetCollection<Project>("collection_Project");
然后我按照 this MongoDB 教程中的说明进行了操作。我根据自己的需要进行了调整,比如-
var documents = await SpeCollection.Find(new Project()).ToListAsync();
但是,我一直有以下错误-
MongoDB.Driver.IMongoCollection does not have a definition for 'Find' and the best override of the extension method [superlong stuff]. Find contains non valid arguments.
如果你想要所有文件,为什么不使用Find all
?
var documents = await SpeCollection.Find(new BsonDocument()).ToListAsync();
使用当前版本的驱动程序 (v2.0),您可以通过传递匹配所有内容的过滤器来做到这一点:
var documents = await SpeCollection.Find(_ => true).ToListAsync();
他们还添加了一个空过滤器 (FilterDefinition.Empty
),它将出现在下一版本的驱动程序 (v2.1) 中:
var documents = await SpeCollection.Find(Builders<Project>.Filter.Empty).ToListAsync();
最简单的方法
检索所有文档-
var documents = SpeCollection.AsQueryable();
也转换为JSON
对象-
var json = Json(documents, JsonRequestBehavior.AllowGet);