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

OptionDescription
autoPluralizationTell Mongoose to auto-pluralize any collection names if it encounters any singular words used as collection slugs.
schemaOptionsCustomize schema options for all Mongoose schemas created internally.
collectionsOptions on a collection-by-collection basis. More
globalsOptions for the Globals collection created by Payload. More
connectOptionsCustomize 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.
disableIndexHintsSet 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
migrationDirCustomize the directory that migrations are stored.
transactionOptionsAn object with configuration properties used in transactions or false which will disable the use of transactions.

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]

Collections Options

You can configure the way the MongoDB adapter works on a collection-by-collection basis, including customizing Mongoose schemaOptions for each collection schema created.

Example:

1
const db = mongooseAdapter({
2
url: 'your-url-here',
3
collections: {
4
users: {
5
//
6
schemaOptions: {
7
strict: false,
8
}
9
}
10
}
11
})

Global Options

Payload automatically creates a single globals collection that correspond with any Payload globals that you define. When you initialize the mongooseAdapter, you can specify settings here for your globals in a similar manner to how you can for collections above. Right now, the only property available is schemaOptions but more may be added in the future.

Next

Postgres