Skip to main content
The MongoDB adapter provides a service interface for MongoDB databases using the official MongoDB Node.js driver.

Installation

MongoDBService

The main service class for MongoDB operations.

Constructor

MongoDBAdapterOptions
required
Configuration options for the MongoDB adapter
Collection | Promise<Collection>
required
MongoDB collection instance or a promise that resolves to one
string
default:"_id"
Name of the id field property
PaginationParams
Pagination settings with default and max page size
boolean | string[]
Allow multiple updates. Can be true, false, or an array of method names like ['create', 'patch', 'remove']
boolean
If true, disables automatic ObjectId conversion for the id field
boolean
If true, uses MongoDB’s estimatedDocumentCount instead of countDocuments for better performance

Service Methods

find

Retrieve multiple documents from the collection.
MongoDBAdapterParams
AdapterQuery
Query filters including $limit, $skip, $sort, $select, and MongoDB query operators
PaginationOptions | false
Override pagination settings for this call
Document[]
MongoDB aggregation pipeline stages
FindOptions
Additional MongoDB-specific find options
Paginated<Result> | Result[]
Returns paginated results or an array depending on pagination settings

get

Retrieve a single document by id.
Id | ObjectId
required
The document id to retrieve
MongoDBAdapterParams
Query parameters
Result
The found document

create

Create one or more new documents.
Data | Data[]
required
Document data to create. Can be a single object or array of objects
MongoDBAdapterParams
InsertOneOptions | BulkWriteOptions
MongoDB-specific insert options
Result | Result[]
The created document(s)

update

Completely replace a document.
Id | ObjectId
required
The document id to update
Data
required
Complete document data to replace with
MongoDBAdapterParams
FindOneAndReplaceOptions
MongoDB-specific replace options
Result
The updated document

patch

Partially update one or multiple documents.
Id | ObjectId | null
required
The document id to patch, or null to patch multiple documents
PatchData
required
Partial data to merge with existing document(s). Supports MongoDB update operators like $set, $inc, etc.
MongoDBAdapterParams
AdapterQuery
Query to filter which documents to patch (when id is null)
FindOneAndUpdateOptions
MongoDB-specific update options
Result | Result[]
The patched document(s)

remove

Remove one or multiple documents.
Id | ObjectId | null
required
The document id to remove, or null to remove multiple documents
MongoDBAdapterParams
AdapterQuery
Query to filter which documents to remove (when id is null)
FindOneAndDeleteOptions | DeleteOptions
MongoDB-specific delete options
Result | Result[]
The removed document(s)

MongoDbAdapter

The base adapter class that MongoDBService extends. Provides internal methods prefixed with _ that bypass hooks.

Methods

getObjectId

Converts an id value to an ObjectId if appropriate.
Id | ObjectId
required
The id value to convert
Id | ObjectId
The converted id (ObjectId if applicable)

getModel

Retrieve the MongoDB collection instance.
Collection
The MongoDB collection

aggregateRaw

Execute a MongoDB aggregation pipeline.
MongoDBAdapterParams
required
Document[]
Aggregation pipeline stages. Use { $feathers: true } as a stage marker to inject Feathers query filters
AggregationCursor
MongoDB aggregation cursor

findRaw

Execute a MongoDB find query without processing results.
FindCursor
MongoDB find cursor

Query Syntax

The MongoDB adapter supports Feathers query syntax and MongoDB-specific operators.

Standard Query Operators

Special Query Parameters

Aggregation Pipeline

Use the pipeline parameter for complex aggregations:

MongoDB Update Operators in patch

ObjectId Converters

Utility functions for working with MongoDB ObjectIds in schemas.

resolveObjectId

Convert a string, number, or ObjectId to ObjectId.

resolveQueryObjectId

Convert ObjectId query parameters including $in, $nin, $ne.

keywordObjectId

Ajv keyword for automatic ObjectId conversion in schemas.

Example