Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

MongoDB

To use Payload with MongoDB, install the package @payloadcms/db-mongodb. It will come with everything you need to store your Payload data in MongoDB.

Then from there, pass it to your Payload Config as follows:

1
import { mongooseAdapter } from '@payloadcms/db-mongodb'
2
3
export default buildConfig({
4
// Your config goes here
5
collections: [
6
// Collections go here
7
],
8
// Configure the Mongoose adapter here
9
db: mongooseAdapter({
10
// Mongoose-specific arguments go here.
11
// URL is required.
12
url: process.env.DATABASE_URI,
13
}),
14
})

Options

Option

Description

autoPluralization

Tell Mongoose to auto-pluralize any collection names if it encounters any singular words used as collection slugs.

connectOptions

Customize MongoDB connection options. Payload will connect to your MongoDB database using default options which you can override and extend to include all the options available to mongoose.

collectionsSchemaOptions

Customize Mongoose schema options for collections.

disableIndexHints

Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination, as it increases the speed of the count function used in that query. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false

migrationDir

Customize the directory that migrations are stored.

transactionOptions

An object with configuration properties used in transactions or false which will disable the use of transactions.

collation

Enable language-specific string comparison with customizable options. Available on MongoDB 3.4+. Defaults locale to "en". Example: { strength: 3 }. For a full list of collation options and their definitions, see the MongoDB documentation.

allowAdditionalKeys

By default, Payload strips all additional keys from MongoDB data that don't exist in the Payload schema. If you have some data that you want to include to the result but it doesn't exist in Payload, you can set this to true. Be careful as Payload access control won't work for this data.

allowIDOnCreate

Set to true to use the id passed in data on the create API operations without using a custom ID field.

disableFallbackSort

Set to true to disable the adapter adding a fallback sort when sorting by non-unique fields, this can affect performance in some cases but it ensures a consistent order of results.

useAlternativeDropDatabase

Set to true to use an alternative dropDatabase implementation that calls collection.deleteMany({}) on every collection instead of sending a raw dropDatabase command. Payload only uses dropDatabase for testing purposes. Defaults to false.

useBigIntForNumberIDs

Set to true to use BigInt for custom ID fields of type 'number'. Useful for databases that don't support double or int32 IDs. Defaults to false.

useJoinAggregations

Set to false to disable join aggregations (which use correlated subqueries) and instead populate join fields via multiple find queries. Defaults to true.

usePipelineInSortLookup

Set to false to disable the use of pipeline in the $lookup aggregation in sorting. Defaults to true.

Access to Mongoose models

After Payload is initialized, this adapter exposes all of your Mongoose models and they are available for you to work with directly.

You can access Mongoose models as follows:

  • Collection models - payload.db.collections[myCollectionSlug]
  • Globals model - payload.db.globals
  • Versions model (both collections and globals) - payload.db.versions[myEntitySlug]

Using other MongoDB implementations

You can import the compatabilityOptions object to get the recommended settings for other MongoDB implementations. Since these databases aren't officially supported by payload, you may still encounter issues even with these settings (please create an issue or PR if you believe these options should be updated):

1
import { mongooseAdapter, compatabilityOptions } from '@payloadcms/db-mongodb'
2
3
export default buildConfig({
4
db: mongooseAdapter({
5
url: process.env.DATABASE_URI,
6
// For example, if you're using firestore:
7
...compatabilityOptions.firestore,
8
}),
9
})

We export compatability options for DocumentDB, Azure Cosmos DB and Firestore. Known limitations:

  • Azure Cosmos DB does not support transactions that update two or more documents in different collections, which is a common case when using Payload (via hooks).
  • Azure Cosmos DB the root config property indexSortableFields must be set to true.
Next

Postgres