[英]Get All 'documents' from MongoDB 'collection'
我需要检索 MongoDB 中我的集合中的所有文档,但我不知道如何进行。 我已经这样宣布了我的“收藏”——
private static IMongoCollection<Project> SpeCollection = db.GetCollection<Project>("collection_Project");
我遵循了这个MongoDB 教程中解释的内容。 我根据我的需要调整了它,比如-
var documents = await SpeCollection.Find(new Project()).ToListAsync();
但是,我一直遇到以下错误-
MongoDB.Driver.IMongoCollection 没有“查找”的定义和扩展方法的最佳覆盖[超长内容]。 Find 包含无效参数。
使用当前版本的驱动程序 (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);
如果您想要所有文档,为什么不使用Find all
?
var documents = await SpeCollection.Find(new BsonDocument()).ToListAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.