Find a Document
On this page
You can retrieve a document by using the Find()
method on a collection object.
Example
Find a Document by Using Builders
The following example uses Builders
to find a document in the restaurants
collection that has a name
field with a value of "Bagels N Buns".
Select the Asynchronous or Synchronous tab to see the corresponding code.
// Creates a filter for all documents that have a "name" value of "Bagels N Buns" var filter = Builders<Restaurant>.Filter .Eq(r => r.Name, "Bagels N Buns"); // Asynchronously retrieves the first document that matches the filter return await _restaurantsCollection.Find(filter).FirstOrDefaultAsync();
For a fully runnable example of using the Find()
method
to asynchronously find one document, see the Asynchronous Find One Example.
// Creates a filter for all documents that have a "name" value of "Bagels N Buns" var filter = Builders<Restaurant>.Filter .Eq(r => r.Name, "Bagels N Buns"); // Retrieves the first document that matches the filter var restaurant = _restaurantsCollection.Find(filter).FirstOrDefault();
For a fully runnable example of using the Find()
method
to synchronously find one document, see the Synchronous Find One Example.
Find a Document by Using LINQ
The following example uses LINQ to find a document in the restaurants
collection that has a name
field with a value of "Bagels N Buns".
Select the Asynchronous or Synchronous tab to see the corresponding code.
return await _restaurantsCollection.AsQueryable() .Where(r => r.Name == "Bagels N Buns").FirstOrDefaultAsync();
For a fully runnable example of using the Find()
method
to asynchronously find one document, see the Asynchronous Find One Example.
var query = _restaurantsCollection.AsQueryable() .Where(r => r.Name == "Bagels N Buns").FirstOrDefault();
For a fully runnable example of using the Find()
method
to synchronously find one document, see the Synchronous Find One Example.
Expected Result
Running any of the preceding full examples prints results similar to the following:
{ "_id" : ObjectId("5eb3d668b31de5d588f42950"), "name" : "Bagels N Buns", "restaurant_id" : "40363427", "cuisine" : "Delicatessen", "address" : {...}, "borough" : "Staten Island", "grades" : [...] }
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 a document using LINQ, see LINQ.