Video Transcript (English)
Welcome! In this video, you’ll learn about the Find() and FindAsync() methods, and how to use the .NET Framework’s LINQ syntax with these methods to find specific documents in a collection.
You’ll also learn how to use the C# driver’s Builders class to create a query filter, and when to use LINQ versus the Builders class.
When retrieving documents with C#, we use the Find or FindAsync method.
In this query, we use LINQ to retrieve documents where the AccountId is equal to the specified string.
We then call FirstOrDefault(), which ensures that the account variable is a single document of type Account.
To confirm that we have retrieved a document, we log the account holder's name in the console.
Now let’s run this code.
The console returns the name Iman Landro, which tells us that they are the owner of the account.
We can perform this query asynchronously by using the FindAsync() method and updating the syntax.
Here, we use the FindAsync method and include the await keyword.
Now, let's say we want to return a list of results. To do this, we append ToList() to our query instead of FirstOrDefault.
In this example, we query the collection for all documents by passing in an underscore arrow function true to the Find() method. Then we cast the documents to a List of type Account.
We then iterate over the list and log each account in the console.
When we run the code, each account is successfully logged in the console.
Before moving on to the Builders class, let’s go over one more thing that we can do with Find and LINQ.
Find returns an IEnumerable, so we can append LINQ methods to the Find call.
Here’s an interesting example that filters documents by the AccountType (which is “checking” in this case). It sorts the results in descending order by the Balance, skips the first 5 results, and limits the returned dataset to the next 20 items.
We loop through the returned documents and log the AccountHolder for each.
When we run this code, each account holder is successfully logged in the console.
Now let’s discuss the Builders class.
With the Builders class, we can construct a filter that we pass to the Find() method. This is useful when working with BsonDocuments, because we can’t use LINQ with unstructured data. We can use the Builders class with C# classes as well, but LINQ provides type safety.
In this example, we use a Builder of type BsonDocument to create a Filter definition where the _id field of the BsonDocument matches a specific ObjectId.
We then pass the filter to the Find() method. This is logically the same as passing in a LINQ expression, but we can specify any field name.
You may have noticed the downside to using Builders: field names are passed as strings. A typo in the field name will not be caught by the compiler and will not result in any runtime errors, but it will lead to unexpected results. This can’t happen when using LINQ with strongly typed classes, which is why it’s the preferred method when working with C# classes.
When we run the code, an account belonging to Joshua Stewart is returned.
Here’s another example. This BsonDocument filter definition specifies all documents that have a balance field value greater than 1000.
We use the FindAsync() method and pass in the filter.
Remember, the asynchronous command requires an async method and the await keyword.
When we run the code, the database returns every account with a balance greater than 1000.
Nice work! Let’s recap what we covered in this video.
We queried for documents by using the Find() and FindAsync() methods. We used LINQ to build our queries and return either the first document or a list by using FirstOrDefault() and ToList(). Finally, we used the Builders class.