简体   繁体   English

从MongoDB“集合”中获取所有“文档”

[英]Get All 'documents' from MongoDB 'collection'

I need to retrieve all the documents that are in my collection in MongoDB, but I cannot figure out how.我需要检索 MongoDB 中我的集合中的所有文档,但我不知道如何进行。 I have declared my 'collection' like this-我已经这样宣布了我的“收藏”——

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

And I followed what is explained in this MongoDB tutorial.我遵循了这个MongoDB 教程中解释的内容。 I adjusted it for my needs, like-我根据我的需要调整了它,比如-

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

However, I keep having the following error-但是,我一直遇到以下错误-

MongoDB.Driver.IMongoCollection does not have a definition for 'Find' and the best override of the extension method [superlong stuff]. MongoDB.Driver.IMongoCollection 没有“查找”的定义和扩展方法的最佳覆盖[超长内容]。 Find contains non valid arguments. Find 包含无效参数。

Using the current version of the driver (v2.0) you can do that by passing a filter that matches everything:使用当前版本的驱动程序 (v2.0),您可以通过传递匹配所有内容的过滤器来实现:

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

They have also added an empty filter ( FilterDefinition.Empty ) which will arrive in the next version of the driver (v2.1):他们还添加了一个空过滤器 ( FilterDefinition.Empty ),它将在驱动程序的下一个版本 (v2.1) 中出现:

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

Simplest Way最简单的方法

Retrieve all the documents-检索所有文件-

var documents = SpeCollection.AsQueryable();

Also convert to JSON object-也转换为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM