Class DB
The main entrypoint for all data access methods of the library
Inheritance
Inherited Members
Namespace: MongoDB.Entities
Assembly: MongoDB.Entities.dll
Syntax
public static class DB
Methods
AllDatabaseNamesAsync(MongoClientSettings)
Gets a list of all database names from the server
Declaration
public static async Task<IEnumerable<string>> AllDatabaseNamesAsync(MongoClientSettings settings)
Parameters
Type | Name | Description |
---|---|---|
MongoClientSettings | settings | A MongoClientSettings object |
Returns
Type | Description |
---|---|
Task<IEnumerable<String>> |
AllDatabaseNamesAsync(String, Int32)
Gets a list of all database names from the server
Declaration
public static Task<IEnumerable<string>> AllDatabaseNamesAsync(string host = "127.0.0.1", int port = 27017)
Parameters
Type | Name | Description |
---|---|---|
String | host | Address of the MongoDB server |
Int32 | port | Port number of the server |
Returns
Type | Description |
---|---|
Task<IEnumerable<String>> |
ChangeDefaultDatabase(String)
Switches the default database at runtime
WARNING: Use at your own risk!!! Might result in entities getting saved in the wrong databases under high concurrency situations.
TIP: Make sure to cancel any watchers (change-streams) before switching the default database.
Declaration
public static void ChangeDefaultDatabase(string name)
Parameters
Type | Name | Description |
---|---|---|
String | name | The name of the database to mark as the new default database |
Collection<T>()
Gets the IMongoCollection for a given IEntity type.
TIP: Try never to use this unless really necessary.
Declaration
public static IMongoCollection<T> Collection<T>()
where T : IEntity
Returns
Type | Description |
---|---|
IMongoCollection<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
CollectionName<T>()
Gets the collection name for a given entity type
Declaration
public static string CollectionName<T>()
where T : IEntity
Returns
Type | Description |
---|---|
String |
Type Parameters
Name | Description |
---|---|
T | The type of entity to get the collection name for |
CountAsync<T>(FilterDefinition<T>, IClientSessionHandle, CancellationToken, CountOptions)
Gets an accurate count of how many total entities are in the collection for a given entity type
Declaration
public static Task<long> CountAsync<T>(FilterDefinition<T> filter, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken), CountOptions options = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
FilterDefinition<T> | filter | A filter definition |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
CountOptions | options | An optional CountOptions object |
Returns
Type | Description |
---|---|
Task<Int64> |
Type Parameters
Name | Description |
---|---|
T | The entity type to get the count for |
CountAsync<T>(IClientSessionHandle, CancellationToken)
Gets an accurate count of how many total entities are in the collection for a given entity type
Declaration
public static Task<long> CountAsync<T>(IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<Int64> |
Type Parameters
Name | Description |
---|---|
T | The entity type to get the count for |
CountAsync<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>, IClientSessionHandle, CancellationToken, CountOptions)
Gets an accurate count of how many total entities are in the collection for a given entity type
Declaration
public static Task<long> CountAsync<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken), CountOptions options = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> | filter | f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value) |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
CountOptions | options | An optional CountOptions object |
Returns
Type | Description |
---|---|
Task<Int64> |
Type Parameters
Name | Description |
---|---|
T | The entity type to get the count for |
CountAsync<T>(Expression<Func<T, Boolean>>, IClientSessionHandle, CancellationToken, CountOptions)
Gets an accurate count of how many entities are matched for a given expression/filter
Declaration
public static Task<long> CountAsync<T>(Expression<Func<T, bool>> expression, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken), CountOptions options = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, Boolean>> | expression | A lambda expression for getting the count for a subset of the data |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
CountOptions | options | An optional CountOptions object |
Returns
Type | Description |
---|---|
Task<Int64> |
Type Parameters
Name | Description |
---|---|
T | The entity type to get the count for |
CountEstimatedAsync<T>(CancellationToken)
Gets a fast estimation of how many documents are in the collection using metadata.
HINT: The estimation may not be exactly accurate.
Declaration
public static Task<long> CountEstimatedAsync<T>(CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<Int64> |
Type Parameters
Name | Description |
---|---|
T | The entity type to get the count for |
CreateCollectionAsync<T>(Action<CreateCollectionOptions<T>>, CancellationToken, IClientSessionHandle)
Creates a collection for an Entity type explicitly using the given options
Declaration
public static Task CreateCollectionAsync<T>(Action<CreateCollectionOptions<T>> options, CancellationToken cancellation = default(CancellationToken), IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Action<CreateCollectionOptions<T>> | options | The options to use for collection creation |
CancellationToken | cancellation | An optional cancellation token |
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
T | The type of entity that will be stored in the created collection |
Database(String)
Gets the IMongoDatabase for a given database name if it has been previously initialized. You can also get the default database by passing 'default' or 'null' for the name parameter.
Declaration
public static IMongoDatabase Database(string name)
Parameters
Type | Name | Description |
---|---|---|
String | name | The name of the database to retrieve |
Returns
Type | Description |
---|---|
IMongoDatabase |
Database<T>()
Gets the IMongoDatabase for the given entity type
Declaration
public static IMongoDatabase Database<T>()
where T : IEntity
Returns
Type | Description |
---|---|
IMongoDatabase |
Type Parameters
Name | Description |
---|---|
T | The type of entity |
DatabaseFor<T>(String)
Specifies the database that a given entity type should be stored in. Only needed for entity types you want stored in a db other than the default db.
Declaration
public static void DatabaseFor<T>(string database)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
String | database | The name of the database |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
DatabaseName<T>()
Gets the name of the database a given entity type is attached to. Returns name of default database if not specifically attached.
Declaration
public static string DatabaseName<T>()
where T : IEntity
Returns
Type | Description |
---|---|
String |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
DeleteAsync<T>(FilterDefinition<T>, IClientSessionHandle, CancellationToken, Collation)
Deletes matching entities with a filter definition
HINT: If the expression matches more than 100,000 entities, they will be deleted in batches of 100k.
HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.
Declaration
public static async Task<DeleteResult> DeleteAsync<T>(FilterDefinition<T> filter, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken), Collation collation = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
FilterDefinition<T> | filter | A filter definition for matching entities to delete. |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Collation | collation | An optional collation object |
Returns
Type | Description |
---|---|
Task<DeleteResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
DeleteAsync<T>(IEnumerable<String>, IClientSessionHandle, CancellationToken)
Deletes entities using a collection of IDs
HINT: If more than 100,000 IDs are passed in, they will be processed in batches of 100k.
HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.
Declaration
public static async Task<DeleteResult> DeleteAsync<T>(IEnumerable<string> IDs, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<String> | IDs | An IEnumerable of entity IDs |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<DeleteResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
DeleteAsync<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>, IClientSessionHandle, CancellationToken, Collation)
Deletes matching entities with a filter expression
HINT: If the expression matches more than 100,000 entities, they will be deleted in batches of 100k.
HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.
Declaration
public static Task<DeleteResult> DeleteAsync<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken), Collation collation = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> | filter | f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value) |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Collation | collation | An optional collation object |
Returns
Type | Description |
---|---|
Task<DeleteResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
DeleteAsync<T>(Expression<Func<T, Boolean>>, IClientSessionHandle, CancellationToken, Collation)
Deletes matching entities with an expression
HINT: If the expression matches more than 100,000 entities, they will be deleted in batches of 100k.
HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.
Declaration
public static Task<DeleteResult> DeleteAsync<T>(Expression<Func<T, bool>> expression, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken), Collation collation = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Expression<Func<T, Boolean>> | expression | A lambda expression for matching entities to delete. |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Collation | collation | An optional collation object |
Returns
Type | Description |
---|---|
Task<DeleteResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
DeleteAsync<T>(String, IClientSessionHandle, CancellationToken)
Deletes a single entity from MongoDB.
HINT: If this entity is referenced by one-to-many/many-to-many relationships, those references are also deleted.
Declaration
public static Task<DeleteResult> DeleteAsync<T>(string ID, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
String | ID | The Id of the entity to delete |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<DeleteResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
Distinct<T, TProperty>(IClientSessionHandle)
Represents a MongoDB Distinct command where you can get back distinct values for a given property of a given Entity.
Declaration
public static Distinct<T, TProperty> Distinct<T, TProperty>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
Distinct<T, TProperty> |
Type Parameters
Name | Description |
---|---|
T | Any Entity that implements IEntity interface |
TProperty | The type of the property of the entity you'd like to get unique values for |
DropCollectionAsync<T>(IClientSessionHandle)
Deletes the collection of a given entity type as well as the join collections for that entity.
TIP: When deleting a collection, all relationships associated with that entity type is also deleted.
Declaration
public static async Task DropCollectionAsync<T>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
T | The entity type to drop the collection of |
Entity<T>()
Returns a new instance of the supplied IEntity type
Declaration
public static T Entity<T>()
where T : IEntity, new()
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
Entity<T>(String)
Returns a new instance of the supplied IEntity type with the ID set to the supplied value
Declaration
public static T Entity<T>(string ID)
where T : IEntity, new()
Parameters
Type | Name | Description |
---|---|---|
String | ID | The ID to set on the returned instance |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
File<T>(String)
Returns a DataStreamer object to enable uploading/downloading file data directly by supplying the ID of the file entity
Declaration
public static DataStreamer File<T>(string ID)
where T : FileEntity, new()
Parameters
Type | Name | Description |
---|---|---|
String | ID | The ID of the file entity |
Returns
Type | Description |
---|---|
DataStreamer |
Type Parameters
Name | Description |
---|---|
T | The file entity type |
Filter<T>()
Exposes the mongodb Filter Definition Builder for a given type.
Declaration
public static FilterDefinitionBuilder<T> Filter<T>()
where T : IEntity
Returns
Type | Description |
---|---|
FilterDefinitionBuilder<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
Find<T>(IClientSessionHandle)
Represents a MongoDB Find command
TIP: Specify your criteria using .Match() .Sort() .Skip() .Take() .Project() .Option() methods and finally call .Execute()
Declaration
public static Find<T> Find<T>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
Find<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
Find<T, TProjection>(IClientSessionHandle)
Represents a MongoDB Find command
TIP: Specify your criteria using .Match() .Sort() .Skip() .Take() .Project() .Option() methods and finally call .Execute()
Declaration
public static Find<T, TProjection> Find<T, TProjection>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
Find<T, TProjection> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
TProjection | The type that is returned by projection |
Fluent<T>(AggregateOptions, IClientSessionHandle)
Exposes the MongoDB collection for the given IEntity as an IAggregateFluent in order to facilitate Fluent queries.
Declaration
public static IAggregateFluent<T> Fluent<T>(AggregateOptions options = null, IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
AggregateOptions | options | The options for the aggregation. This is not required. |
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
IAggregateFluent<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
FluentGeoNear<T>(Coordinates2D, Expression<Func<T, Object>>, Boolean, Nullable<Double>, Nullable<Double>, Nullable<Int32>, BsonDocument, Nullable<Double>, Expression<Func<T, Object>>, String, AggregateOptions, IClientSessionHandle)
Start a fluent aggregation pipeline with a $GeoNear stage with the supplied parameters.
Declaration
public static IAggregateFluent<T> FluentGeoNear<T>(Coordinates2D NearCoordinates, Expression<Func<T, object>> DistanceField, bool Spherical = true, double? MaxDistance = null, double? MinDistance = null, int? Limit = null, BsonDocument Query = null, double? DistanceMultiplier = null, Expression<Func<T, object>> IncludeLocations = null, string IndexKey = null, AggregateOptions options = null, IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Coordinates2D | NearCoordinates | The coordinates from which to find documents from |
Expression<Func<T, Object>> | DistanceField | x => x.Distance |
Boolean | Spherical | Calculate distances using spherical geometry or not |
Nullable<Double> | MaxDistance | The maximum distance in meters from the center point that the documents can be |
Nullable<Double> | MinDistance | The minimum distance in meters from the center point that the documents can be |
Nullable<Int32> | Limit | The maximum number of documents to return |
BsonDocument | Query | Limits the results to the documents that match the query |
Nullable<Double> | DistanceMultiplier | The factor to multiply all distances returned by the query |
Expression<Func<T, Object>> | IncludeLocations | Specify the output field to store the point used to calculate the distance |
String | IndexKey | |
AggregateOptions | options | The options for the aggregation. This is not required. |
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
IAggregateFluent<T> |
Type Parameters
Name | Description |
---|---|
T |
FluentTextSearch<T>(Search, String, Boolean, Boolean, String, AggregateOptions, IClientSessionHandle)
Start a fluent aggregation pipeline with a $text stage with the supplied parameters.
TIP: Make sure to define a text index with DB.Index<T>() before searching
Declaration
public static IAggregateFluent<T> FluentTextSearch<T>(Search searchType, string searchTerm, bool caseSensitive = false, bool diacriticSensitive = false, string language = null, AggregateOptions options = null, IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Search | searchType | The type of text matching to do |
String | searchTerm | The search term |
Boolean | caseSensitive | Case sensitivity of the search (optional) |
Boolean | diacriticSensitive | Diacritic sensitivity of the search (optional) |
String | language | The language for the search (optional) |
AggregateOptions | options | Options for finding documents (not required) |
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
IAggregateFluent<T> |
Type Parameters
Name | Description |
---|---|
T |
Index<T>()
Represents an index for a given IEntity
TIP: Define the keys first with .Key() method and finally call the .Create() method.
Declaration
public static Index<T> Index<T>()
where T : IEntity
Returns
Type | Description |
---|---|
Index<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
InitAsync(String, MongoClientSettings)
Initializes a MongoDB connection with the given connection parameters.
WARNING: will throw an error if server is not reachable!
You can call this method as many times as you want (such as in serverless functions) with the same parameters and the connections won't get duplicated.Declaration
public static Task InitAsync(string database, MongoClientSettings settings)
Parameters
Type | Name | Description |
---|---|---|
String | database | Name of the database |
MongoClientSettings | settings | A MongoClientSettings object |
Returns
Type | Description |
---|---|
Task |
InitAsync(String, String, Int32)
Initializes a MongoDB connection with the given connection parameters.
WARNING: will throw an error if server is not reachable!
You can call this method as many times as you want (such as in serverless functions) with the same parameters and the connections won't get duplicated.Declaration
public static Task InitAsync(string database, string host = "127.0.0.1", int port = 27017)
Parameters
Type | Name | Description |
---|---|---|
String | database | Name of the database |
String | host | Address of the MongoDB server |
Int32 | port | Port number of the server |
Returns
Type | Description |
---|---|
Task |
InsertAsync<T>(T, IClientSessionHandle, CancellationToken)
Inserts a new entity into the colleciton.
Declaration
public static Task InsertAsync<T>(T entity, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
T | entity | The instance to persist |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | And optional cancellation token |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
InsertAsync<T>(IEnumerable<T>, IClientSessionHandle, CancellationToken)
Inserts a batch of new entities into the collection.
Declaration
public static Task<BulkWriteResult<T>> InsertAsync<T>(IEnumerable<T> entities, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | entities | The entities to persist |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | And optional cancellation token |
Returns
Type | Description |
---|---|
Task<BulkWriteResult<T>> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
MigrateAsync()
Executes migration classes that implement the IMigration interface in the correct order to transform the database.
TIP: Write classes with names such as: _001_rename_a_field.cs, _002_delete_a_field.cs, etc. and implement IMigration interface on them. Call this method at the startup of the application in order to run the migrations.
Declaration
public static Task MigrateAsync()
Returns
Type | Description |
---|---|
Task |
MigrateAsync<T>()
Discover and run migrations from the same assembly as the specified type.
Declaration
public static Task MigrateAsync<T>()
where T : class
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
T | A type that is from the same assembly as the migrations you want to run |
MigrationsAsync(IEnumerable<IMigration>)
Executes the given collection of IMigrations in the correct order to transform the database.
Declaration
public static Task MigrationsAsync(IEnumerable<IMigration> migrations)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<IMigration> | migrations | The collection of migrations to execute |
Returns
Type | Description |
---|---|
Task |
NextSequentialNumberAsync(String, CancellationToken)
Returns an atomically generated sequential number for the given sequence name everytime the method is called
Declaration
public static Task<ulong> NextSequentialNumberAsync(string sequenceName, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
String | sequenceName | The name of the sequence to get the next number for |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<UInt64> |
NextSequentialNumberAsync<T>(CancellationToken)
Returns an atomically generated sequential number for the given Entity type everytime the method is called
Declaration
public static Task<ulong> NextSequentialNumberAsync<T>(CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<UInt64> |
Type Parameters
Name | Description |
---|---|
T | The type of entity to get the next sequential number for |
PagedSearch<T>(IClientSessionHandle)
Represents an aggregation query that retrieves results with easy paging support.
Declaration
public static PagedSearch<T> PagedSearch<T>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
PagedSearch<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
PagedSearch<T, TProjection>(IClientSessionHandle)
Represents an aggregation query that retrieves results with easy paging support.
Declaration
public static PagedSearch<T, TProjection> PagedSearch<T, TProjection>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
PagedSearch<T, TProjection> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
TProjection | The type you'd like to project the results to. |
PipelineAsync<T, TResult>(Template<T, TResult>, AggregateOptions, IClientSessionHandle, CancellationToken)
Executes an aggregation pipeline by supplying a 'Template' object and get a list of results
Declaration
public static async Task<List<TResult>> PipelineAsync<T, TResult>(Template<T, TResult> template, AggregateOptions options = null, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Template<T, TResult> | template | A 'Template' object with tags replaced |
AggregateOptions | options | The options for the aggregation. This is not required. |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<List<TResult>> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
TResult | The type of the resulting objects |
PipelineCursorAsync<T, TResult>(Template<T, TResult>, AggregateOptions, IClientSessionHandle, CancellationToken)
Executes an aggregation pipeline by supplying a 'Template' object and returns a cursor
Declaration
public static Task<IAsyncCursor<TResult>> PipelineCursorAsync<T, TResult>(Template<T, TResult> template, AggregateOptions options = null, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Template<T, TResult> | template | A 'Template' object with tags replaced |
AggregateOptions | options | The options for the aggregation. This is not required. |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<IAsyncCursor<TResult>> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
TResult | The type of the resulting objects |
PipelineFirstAsync<T, TResult>(Template<T, TResult>, AggregateOptions, IClientSessionHandle, CancellationToken)
Executes an aggregation pipeline by supplying a 'Template' object and get the first result or default value if not found.
Declaration
public static async Task<TResult> PipelineFirstAsync<T, TResult>(Template<T, TResult> template, AggregateOptions options = null, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Template<T, TResult> | template | A 'Template' object with tags replaced |
AggregateOptions | options | The options for the aggregation. This is not required. |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<TResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
TResult | The type of the resulting object |
PipelineSingleAsync<T, TResult>(Template<T, TResult>, AggregateOptions, IClientSessionHandle, CancellationToken)
Executes an aggregation pipeline by supplying a 'Template' object and get a single result or default value if not found. If more than one entity is found, it will throw an exception.
Declaration
public static async Task<TResult> PipelineSingleAsync<T, TResult>(Template<T, TResult> template, AggregateOptions options = null, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
Template<T, TResult> | template | A 'Template' object with tags replaced |
AggregateOptions | options | The options for the aggregation. This is not required. |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<TResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
TResult | The type of the resulting object |
Projection<T>()
Exposes the mongodb Projection Definition Builder for a given type.
Declaration
public static ProjectionDefinitionBuilder<T> Projection<T>()
where T : IEntity
Returns
Type | Description |
---|---|
ProjectionDefinitionBuilder<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
Queryable<T>(AggregateOptions, IClientSessionHandle)
Exposes the MongoDB collection for the given IEntity as an IQueryable in order to facilitate LINQ queries.
Declaration
public static IMongoQueryable<T> Queryable<T>(AggregateOptions options = null, IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
AggregateOptions | options | The aggregate options |
IClientSessionHandle | session | An optional session if used within a transaction |
Returns
Type | Description |
---|---|
IMongoQueryable<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
Replace<T>(IClientSessionHandle)
Represents a ReplaceOne command, which can replace the first matched document with a given entity
TIP: Specify a filter first with the .Match(). Then set entity with .WithEntity() and finally call .Execute() to run the command.
Declaration
public static Replace<T> Replace<T>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
Replace<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveAsync<T>(T, IClientSessionHandle, CancellationToken)
Saves a complete entity replacing an existing entity or creating a new one if it does not exist. If ID value is null, a new entity is created. If ID has a value, then existing entity is replaced.
Declaration
public static Task SaveAsync<T>(T entity, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
T | entity | The instance to persist |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | And optional cancellation token |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveAsync<T>(IEnumerable<T>, IClientSessionHandle, CancellationToken)
Saves a batch of complete entities replacing existing ones or creating new ones if they do not exist. If ID value is null, a new entity is created. If ID has a value, then existing entity is replaced.
Declaration
public static Task<BulkWriteResult<T>> SaveAsync<T>(IEnumerable<T> entities, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | entities | The entities to persist |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | And optional cancellation token |
Returns
Type | Description |
---|---|
Task<BulkWriteResult<T>> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveExceptAsync<T>(T, IEnumerable<String>, IClientSessionHandle, CancellationToken)
Saves an entity partially excluding the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with an IEnumerable. Property names must match exactly.
Declaration
public static Task<UpdateResult> SaveExceptAsync<T>(T entity, IEnumerable<string> propNames, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
T | entity | The entity to save |
IEnumerable<String> | propNames | new List { "PropOne", "PropTwo" } |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<UpdateResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveExceptAsync<T>(T, Expression<Func<T, Object>>, IClientSessionHandle, CancellationToken)
Saves an entity partially excluding the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be excluded can be specified with a 'New' expression. You can only specify root level properties with the expression.
Declaration
public static Task<UpdateResult> SaveExceptAsync<T>(T entity, Expression<Func<T, object>> members, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
T | entity | The entity to save |
Expression<Func<T, Object>> | members | x => new { x.PropOne, x.PropTwo } |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<UpdateResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveExceptAsync<T>(IEnumerable<T>, IEnumerable<String>, IClientSessionHandle, CancellationToken)
Saves a batch of entities partially excluding the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with an IEnumerable. Property names must match exactly.
Declaration
public static Task<BulkWriteResult<T>> SaveExceptAsync<T>(IEnumerable<T> entities, IEnumerable<string> propNames, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | entities | The batch of entities to save |
IEnumerable<String> | propNames | new List { "PropOne", "PropTwo" } |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<BulkWriteResult<T>> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveExceptAsync<T>(IEnumerable<T>, Expression<Func<T, Object>>, IClientSessionHandle, CancellationToken)
Saves a batch of entities partially excluding the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be excluded can be specified with a 'New' expression. You can only specify root level properties with the expression.
Declaration
public static Task<BulkWriteResult<T>> SaveExceptAsync<T>(IEnumerable<T> entities, Expression<Func<T, object>> members, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | entities | The batch of entities to save |
Expression<Func<T, Object>> | members | x => new { x.PropOne, x.PropTwo } |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<BulkWriteResult<T>> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveOnlyAsync<T>(T, IEnumerable<String>, IClientSessionHandle, CancellationToken)
Saves an entity partially with only the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with an IEnumerable. Property names must match exactly.
Declaration
public static Task<UpdateResult> SaveOnlyAsync<T>(T entity, IEnumerable<string> propNames, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
T | entity | The entity to save |
IEnumerable<String> | propNames | new List { "PropOne", "PropTwo" } |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<UpdateResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveOnlyAsync<T>(T, Expression<Func<T, Object>>, IClientSessionHandle, CancellationToken)
Saves an entity partially with only the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with a 'New' expression. You can only specify root level properties with the expression.
Declaration
public static Task<UpdateResult> SaveOnlyAsync<T>(T entity, Expression<Func<T, object>> members, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
T | entity | The entity to save |
Expression<Func<T, Object>> | members | x => new { x.PropOne, x.PropTwo } |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<UpdateResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveOnlyAsync<T>(IEnumerable<T>, IEnumerable<String>, IClientSessionHandle, CancellationToken)
Saves a batch of entities partially with only the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with an IEnumerable. Property names must match exactly.
Declaration
public static Task<BulkWriteResult<T>> SaveOnlyAsync<T>(IEnumerable<T> entities, IEnumerable<string> propNames, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | entities | The batch of entities to save |
IEnumerable<String> | propNames | new List { "PropOne", "PropTwo" } |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<BulkWriteResult<T>> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SaveOnlyAsync<T>(IEnumerable<T>, Expression<Func<T, Object>>, IClientSessionHandle, CancellationToken)
Saves a batch of entities partially with only the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with a 'New' expression. You can only specify root level properties with the expression.
Declaration
public static Task<BulkWriteResult<T>> SaveOnlyAsync<T>(IEnumerable<T> entities, Expression<Func<T, object>> members, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | entities | The batch of entities to save |
Expression<Func<T, Object>> | members | x => new { x.PropOne, x.PropTwo } |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<BulkWriteResult<T>> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
SavePreservingAsync<T>(T, IClientSessionHandle, CancellationToken)
Saves an entity partially while excluding some properties. The properties to be excluded can be specified using the [Preserve] or [DontPreserve] attributes.
Declaration
public static Task<UpdateResult> SavePreservingAsync<T>(T entity, IClientSessionHandle session = null, CancellationToken cancellation = default(CancellationToken))
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
T | entity | The entity to save |
IClientSessionHandle | session | An optional session if using within a transaction |
CancellationToken | cancellation | An optional cancellation token |
Returns
Type | Description |
---|---|
Task<UpdateResult> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
Sort<T>()
Exposes the mongodb Sort Definition Builder for a given type.
Declaration
public static SortDefinitionBuilder<T> Sort<T>()
where T : IEntity
Returns
Type | Description |
---|---|
SortDefinitionBuilder<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
Transaction(String, ClientSessionOptions, ModifiedBy)
Gets a transaction context/scope for a given database or the default database if not specified.
Declaration
public static Transaction Transaction(string database = null, ClientSessionOptions options = null, ModifiedBy modifiedBy = null)
Parameters
Type | Name | Description |
---|---|---|
String | database | The name of the database which this transaction is for (not required) |
ClientSessionOptions | options | Client session options (not required) |
ModifiedBy | modifiedBy |
Returns
Type | Description |
---|---|
Transaction |
Transaction<T>(ClientSessionOptions, ModifiedBy)
Gets a transaction context/scope for a given entity type's database
Declaration
public static Transaction Transaction<T>(ClientSessionOptions options = null, ModifiedBy modifiedBy = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
ClientSessionOptions | options | Client session options (not required) |
ModifiedBy | modifiedBy |
Returns
Type | Description |
---|---|
Transaction |
Type Parameters
Name | Description |
---|---|
T | The entity type to determine the database from for the transaction |
Update<T>(IClientSessionHandle)
Represents an update command
TIP: Specify a filter first with the .Match() method. Then set property values with .Modify() and finally call .Execute() to run the command.
Declaration
public static Update<T> Update<T>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
Update<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
UpdateAndGet<T>(IClientSessionHandle)
Update and retrieve the first document that was updated.
TIP: Specify a filter first with the .Match(). Then set property values with .Modify() and finally call .Execute() to run the command.
Declaration
public static UpdateAndGet<T> UpdateAndGet<T>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
UpdateAndGet<T> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
UpdateAndGet<T, TProjection>(IClientSessionHandle)
Update and retrieve the first document that was updated.
TIP: Specify a filter first with the .Match(). Then set property values with .Modify() and finally call .Execute() to run the command.
Declaration
public static UpdateAndGet<T, TProjection> UpdateAndGet<T, TProjection>(IClientSessionHandle session = null)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
IClientSessionHandle | session | An optional session if using within a transaction |
Returns
Type | Description |
---|---|
UpdateAndGet<T, TProjection> |
Type Parameters
Name | Description |
---|---|
T | Any class that implements IEntity |
TProjection | The type to project to |
Watcher<T>(String)
Retrieves the 'change-stream' watcher instance for a given unique name. If an instance for the name does not exist, it will return a new instance. If an instance already exists, that instance will be returned.
Declaration
public static Watcher<T> Watcher<T>(string name)
where T : IEntity
Parameters
Type | Name | Description |
---|---|---|
String | name | A unique name for the watcher of this entity type. Names can be duplicate among different entity types. |
Returns
Type | Description |
---|---|
Watcher<T> |
Type Parameters
Name | Description |
---|---|
T | The entity type to get a watcher for |
Watchers<T>()
Returns all the watchers for a given entity type
Declaration
public static IEnumerable<Watcher<T>> Watchers<T>()
where T : IEntity
Returns
Type | Description |
---|---|
IEnumerable<Watcher<T>> |
Type Parameters
Name | Description |
---|---|
T | The entity type to get the watcher of |