# Class MongoRepository<T>

Namespace
  adas_core.Infrastructure.Repositories

Assembly
  adas-core.Infrastructure.dll

Abstract base repository providing common MongoDB persistence operations for a given document type `T`. Concrete repositories must implement GetCollectionName() to identify the target collection, and may override CreateIndexes(), InsertInitialLoad(), and InsertOneAsync(T) to customize schema setup, seeding, and insertion behavior.

    public abstract class MongoRepository<T> : IMongoRepository<T>

#### Type Parameters

`T`

  The domain model type persisted in the MongoDB collection.

Inheritance
  object
  MongoRepository<T>

Implements
  IMongoRepository<T>

Derived
  AdmissionRepository
  AlarmRepository
  AppointmentArchiveRepository
  AppointmentRepository
  ArchivePatientCarePlanRepository
  AuthorityRepository
  CameraRepository
  ConfigObservationRepository
  ConfigPumpsRepository
  ConfigUnitsRepository
  DeviceRepository
  DiagnosisArchiveRepository
  DiagnosisRepository
  DischargeRepository
  DisplayCardConfigRepository
  DisplayChartConfigRepository
  DisplayConfigRepository
  DisplayDetailConfigRepository
  DisplayRepository
  HistoricalConfigChangesRepository
  LightBeaconRepository
  MasterListRepository<T>
  MedicineRepository
  NoticeRepository
  ObservationArchiveRepository
  ObservationRepository
  PatientArchiveRepository
  PatientCarePlanRepository
  PatientRepository
  PoCMappingRepository
  PoCSettingsRepository
  PointOfCareRepository
  PumpAlarmEventRepository
  PumpAlarmStateRepository
  PumpArchiveRepository
  PumpObservationRepository
  PumpStateRepository
  RecordingAlertArchiveRepository
  RecordingAlertRepository
  RelayRepository
  SectionRepository
  ServiceConfigRepository
  TreatmentArchiveRepository
  TreatmentRepository
  UnitRepository
  UserRepository
  LocalLoginRepository

Inherited Members
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.GetType()
  object.MemberwiseClone()
  object.ReferenceEquals(object, object)
  object.ToString()

Extension Methods
  NumberUtils.IsNumber(object)
  NumberUtils.ToDouble(object)
  ObjectUtils.ToStr(object?)

## Constructors

### MongoRepository(IMongoDatabase)

Initializes a new instance of the MongoRepository<T> class using the provided database.

    protected MongoRepository(IMongoDatabase database)

#### Parameters

`database` IMongoDatabase

  The MongoDB database instance used to access collections. Must not be null.

## Fields

### Db

The MongoDB database instance used by the repository.

    protected readonly IMongoDatabase Db

#### Field Value

IMongoDatabase

## Properties

### Collection

Gets the underlying MongoDB.Driver.IMongoCollection<TDocument> for the repository. On first access, ensures the collection exists (creating it if necessary), then triggers asynchronous index creation and initial data loading via CreateIndexes() and InsertInitialLoad().

    public IMongoCollection<T> Collection { get; set; }

#### Property Value

IMongoCollection<T>

  The MongoDB collection of `T` documents.

## Methods

### CollectionExists(string)

Determines whether a collection with the specified name exists in the current database. Errors are logged and the method returns false in that case.

    protected bool CollectionExists(string collectionName)

#### Parameters

`collectionName` string

  The name of the collection to check.

#### Returns

bool

  true if a collection with the given name exists; otherwise, false. Returns false when an exception is thrown while querying the database.

### CreateIndexes()

Creates the indexes required for the collection. The base implementation is a no-op; derived classes should override this method to define their own indexes.

    public virtual Task CreateIndexes()

#### Returns

Task

  A Task representing the asynchronous index creation operation.

### DeleteAsync(ObjectId)

Asynchronously finds and deletes a single document identified by its `_id`. Returns the deleted document, or the default value of `T` if not found or on error.

    public Task<T?> DeleteAsync(ObjectId id)

#### Parameters

`id` ObjectId

  The MongoDB.Bson.ObjectId value of the document's `_id` field.

#### Returns

Task<T>

  A Task<TResult> representing the asynchronous operation. The task result contains the deleted document, or null / default if no document matched or an error occurred.

### DeleteAsync(string)

Asynchronously finds and deletes a single document identified by a string `_id`. Returns the deleted document, or the default value of `T` if not found or on error.

    protected Task<T?> DeleteAsync(string id)

#### Parameters

`id` string

  The string value of the document's `_id` field.

#### Returns

Task<T>

  A Task<TResult> representing the asynchronous operation. The task result contains the deleted document, or null / default if no document matched or an error occurred.

### GetCollectionName()

When implemented in a derived class, returns the name of the MongoDB collection used to store documents of type `T`.

    public abstract string GetCollectionName()

#### Returns

string

  The MongoDB collection name as a string.

### InsertInitialLoad()

Performs an initial data load (seeding) for the collection. The base implementation is a no-op; derived classes should override this method to provide custom seeding logic.

    public virtual Task InsertInitialLoad()

#### Returns

Task

  A Task representing the asynchronous seeding operation.

### InsertManyAsync(List<T>)

Asynchronously inserts multiple documents into the collection using unordered semantics (a single failed insert does not abort the batch). Errors are logged and swallowed.

    public virtual Task InsertManyAsync(List<T> obj)

#### Parameters

`obj` List<T>

  The list of documents of type `T` to insert.

#### Returns

Task

  A Task representing the asynchronous bulk insert operation.

### InsertOneAsync(T)

Asynchronously inserts a single document into the collection. Errors are logged and swallowed.

    public virtual Task InsertOneAsync(T obj)

#### Parameters

`obj` T

  The document of type `T` to insert.

#### Returns

Task

  A Task representing the asynchronous insert operation.

### UpdateManyObjectIdAsync(string, ObjectId, ObjectId)

Asynchronously updates all documents in the collection where the specified field equals `oldId`, setting that field to the new `id`. Errors are logged and swallowed.

    protected Task UpdateManyObjectIdAsync(string nameId, ObjectId id, ObjectId oldId)

#### Parameters

`nameId` string

  The name of the field to match and update.

`id` ObjectId

  The new MongoDB.Bson.ObjectId value to assign.

`oldId` ObjectId

  The existing MongoDB.Bson.ObjectId value to replace.

#### Returns

Task

  A Task representing the asynchronous update operation.

### UpdateOneAsync(ObjectId, T)

Asynchronously replaces (or upserts) a single document identified by its `_id`. Uses MongoDB.Driver.ReplaceOptions with MongoDB.Driver.ReplaceOptions.IsUpsert set to true so that the document is created if it does not exist. Errors are logged and swallowed.

    public Task UpdateOneAsync(ObjectId id, T obj)

#### Parameters

`id` ObjectId

  The MongoDB.Bson.ObjectId value of the document's `_id` field.

`obj` T

  The replacement document of type `T`.

#### Returns

Task

  A Task representing the asynchronous replace/upsert operation.