Table of Contents

Class MongoDbRepositoryBase<TDocument>

Namespace
Excalibur.Data.MongoDB
Assembly
Excalibur.Data.MongoDB.dll

Provides a base implementation for interacting with MongoDB for a specific document type.

public abstract class MongoDbRepositoryBase<TDocument> : IMongoDbRepositoryBase<TDocument>, IMongoDbRepositoryBaseQuery<TDocument> where TDocument : class

Type Parameters

TDocument

The type of the document to manage in MongoDB.

Inheritance
MongoDbRepositoryBase<TDocument>
Implements
Inherited Members

Examples

public class OrderSearchRepository : MongoDbRepositoryBase<OrderProjection>
{
    public OrderSearchRepository(IMongoClient client, IOptionsMonitor<MongoDbProjectionStoreOptions> options)
        : base(client,
               options.Get(MongoDbProjectionCollectionConvention.GetOptionsName<OrderProjection>()).DatabaseName,
               MongoDbProjectionCollectionConvention.GetCollectionName<OrderProjection>(
                   options.Get(MongoDbProjectionCollectionConvention.GetOptionsName<OrderProjection>())))
    {
    }

    public override Task InitializeCollectionAsync(CancellationToken ct) => Task.CompletedTask;
}

Remarks

This class includes operations for adding, updating, retrieving, deleting, and querying documents, as well as initializing collections and indexes in MongoDB.

Documents are stored as flat BSON documents. The MongoDB driver's IgnoreExtraElementsConvention is registered globally so that consumer document types silently ignore metadata fields during deserialization.

Use this base class to build custom query repositories that share MongoDB collections with IProjectionStore<T> or other stores.

Constructors

MongoDbRepositoryBase(IMongoClient, string, string)

Initializes a new instance of the MongoDbRepositoryBase<TDocument> class.

protected MongoDbRepositoryBase(IMongoClient client, string databaseName, string collectionName)

Parameters

client IMongoClient

The MongoDB client instance.

databaseName string

The name of the database to operate on.

collectionName string

The name of the collection to operate on.

Properties

Client

Gets the underlying MongoDB client.

protected IMongoClient Client { get; }

Property Value

IMongoClient

Collection

Gets the underlying MongoDB collection typed as MongoDB.Bson.BsonDocument.

protected IMongoCollection<BsonDocument> Collection { get; }

Property Value

IMongoCollection<BsonDocument>

CollectionName

Gets the name of the collection this repository operates on.

protected string CollectionName { get; }

Property Value

string

Methods

AddOrUpdateAsync(string, TDocument, CancellationToken)

Adds or updates a document in the MongoDB collection.

public virtual Task<bool> AddOrUpdateAsync(string documentId, TDocument document, CancellationToken cancellationToken)

Parameters

documentId string

The unique identifier for the document.

document TDocument

The document to be added or updated.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<bool>

true if the operation was acknowledged.

FindAsync(FilterDefinition<BsonDocument>, SortDefinition<BsonDocument>?, int?, int?, CancellationToken)

Executes a find query against the MongoDB collection.

public virtual Task<IReadOnlyList<TDocument>> FindAsync(FilterDefinition<BsonDocument> filter, SortDefinition<BsonDocument>? sort, int? skip, int? limit, CancellationToken cancellationToken)

Parameters

filter FilterDefinition<BsonDocument>

The filter definition for the query.

sort SortDefinition<BsonDocument>

Optional sort definition.

skip int?

Optional number of documents to skip.

limit int?

Optional maximum number of documents to return.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<IReadOnlyList<TDocument>>

A read-only list of matched documents.

GetByIdAsync(string, CancellationToken)

Retrieves a document from MongoDB by its ID.

public virtual Task<TDocument?> GetByIdAsync(string documentId, CancellationToken cancellationToken)

Parameters

documentId string

The unique identifier of the document.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<TDocument>

The document if found, or null if not found.

InitializeCollectionAsync(CancellationToken)

Initializes the MongoDB collection (creates indexes, etc.).

public abstract Task InitializeCollectionAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The cancellation token.

Returns

Task

A Task representing the asynchronous operation.

RemoveAsync(string, CancellationToken)

Removes a document from the MongoDB collection.

public virtual Task<bool> RemoveAsync(string documentId, CancellationToken cancellationToken)

Parameters

documentId string

The unique identifier of the document to remove.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<bool>

true if the document was removed; false if not found.