Read documents
Find a document
To search for a document in MongoDB you use the Find
method on a IMongoCollection<T>
reference. Find
method accepts a FilterDefinition<T>
parameter where T
is the collection's type.
Filters can be created using the Builders<T>.Filter
definition builder which contain multiple filters. The following example finds a user document based on its Id. It does this using the equality filter Eq<T>
on the id field.
Notice that when filter used with BsonDocument
, _id field name used instead of Id which is the property name on the User
class. When you use Builders<User>.Filter
this is done automatically for you by the driver, based on the Id serialization settings
Find multiple documents
To search for multiple documents follow the same process but this time use the ToList
method. The following example finds all documents with female gender.
Last updated