Find Multiple Documents
On this page
You can retrieve multiple documents from a collection by using the
Find()
method.
Example
Find Documents by Using Builders
The following example uses Builders
to find documents in
the restaurants
collection with the cuisine
"Pizza".
Select the Asynchronous or Synchronous tab to see the corresponding code.
// Creates a filter for all documents that have a "cuisine" value of "Pizza" var filter = Builders<Restaurant>.Filter .Eq(r => r.Cuisine, "Pizza"); // Asynchronously retrieves all documents that match the filter return await _restaurantsCollection.Find(filter).ToListAsync();
For a fully runnable example of using the Find()
method to asynchronously
find multiple documents, see
Asynchronous Find Multiple Example.
// Creates a filter for all documents that have a "cuisine" value of "Pizza" var filter = Builders<Restaurant>.Filter .Eq("cuisine", "Pizza"); // Retrieves all documents that match the filter return _restaurantsCollection.Find(filter).ToList();
For a fully runnable example of using the Find()
method to synchronously
find multiple documents, see
Synchronous Find Multiple Example.
Find Documents by Using LINQ
The following example uses LINQ to find documents in the
restaurants
collection with the cuisine
"Pizza".
Select the Asynchronous or Synchronous tab to see the corresponding code.
return await _restaurantsCollection.AsQueryable() .Where(r => r.Cuisine == "Pizza").ToListAsync();
For a fully runnable example of using the Find()
method to asynchronously
find multiple documents, see
Asynchronous Find Multiple Example.
return _restaurantsCollection.AsQueryable() .Where(r => r.Cuisine == "Pizza").ToList();
For a fully runnable example of using the Find()
method to synchronously
find multiple documents, see
Synchronous Find Multiple Example.
Find All Documents
The following example finds all documents in the restaurants
collection.
Select the Asynchronous or Synchronous tab to see the corresponding code.
var filter = Builders<Restaurant>.Filter.Empty; return await _restaurantsCollection.Find(filter) .ToListAsync();
For a fully runnable example of using the Find()
method to asynchronously
find multiple documents, see
Asynchronous Find Multiple Example.
var filter = Builders<Restaurant>.Filter.Empty; return _restaurantsCollection.Find(filter) .ToList();
For a fully runnable example of using the Find()
method to synchronously
find multiple documents, see
Synchronous Find Multiple Example.
Expected Result
Running the preceding full examples prints the following results:
Finding documents with builders...: Number of documents found: 1163 Finding documents with LINQ...: Number of documents found: 1163 Finding all documents...: Number of documents found: 25359
Tip
Sample Datasets
These examples use the sample datasets provided by Atlas. The number of documents returned may differ depending on the data in your collection.
Additional Information
To learn more about retrieving documents, see the Retrieve Data guide.
To learn more about using builders, see Operations with Builders.
To learn how to find documents using LINQ, see LINQ.