Get All 'documents' from MongoDB 'collection'

前端 未结 3 1779
谎友^
谎友^ 2021-02-05 01:40

I need to retrieve all the documents that are in my collection in MongoDB, but I cannot figure out how. I have declared my \'collection\' like this-

private stat         


        
相关标签:
3条回答
  • 2021-02-05 02:05

    Using the current version of the driver (v2.0) you can do that by passing a filter that matches everything:

    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):

    var documents = await SpeCollection.Find(Builders<Project>.Filter.Empty).ToListAsync();
    
    0 讨论(0)
  • 2021-02-05 02:16

    Simplest Way

    Retrieve all the documents-

    var documents = SpeCollection.AsQueryable();
    

    Also convert to JSON object-

    var json = Json(documents, JsonRequestBehavior.AllowGet);
    
    0 讨论(0)
  • 2021-02-05 02:25

    If you want all documents, why not use Find all?

    var documents = await SpeCollection.Find(new BsonDocument()).ToListAsync();
    
    0 讨论(0)
提交回复
热议问题