This change makes so that data that exists in MongoDB but isn't defined in the Payload config won't be included to `payload.find` / `payload.db.find` calls. Now we strip all the additional keys. Consider you have a field named `secretField` that's also `hidden: true` (or `read: () => false`) that contains some sensitive data. Then you removed this field from the database and as for now with the MongoDB adapter this field will be included to the Local API / REST API results without any consideration, as Payload doesn't know about it anymore. This also fixes https://github.com/payloadcms/payload/issues/11542 if you removed / renamed a relationship field from the schema, Payload won't sanitize ObjectIDs back to strings anymore. Ideally you should create a migration script that completely removes the deleted field from the database with `$unset`, but people rarely do this. If you still need to keep those fields to the result, this PR allows you to do this with the new `allowAdditionalKeys: true` flag.
61 lines
5.4 KiB
Plaintext
61 lines
5.4 KiB
Plaintext
---
|
|
title: MongoDB
|
|
label: MongoDB
|
|
order: 40
|
|
desc: Payload has supported MongoDB natively since we started. The flexible nature of MongoDB lends itself well to Payload's powerful fields.
|
|
keywords: MongoDB, documentation, typescript, Content Management System, cms, headless, javascript, node, react, nextjs
|
|
---
|
|
|
|
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:
|
|
|
|
```ts
|
|
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
|
|
export default buildConfig({
|
|
// Your config goes here
|
|
collections: [
|
|
// Collections go here
|
|
],
|
|
// Configure the Mongoose adapter here
|
|
db: mongooseAdapter({
|
|
// Mongoose-specific arguments go here.
|
|
// URL is required.
|
|
url: process.env.DATABASE_URI,
|
|
}),
|
|
})
|
|
```
|
|
|
|
## Options
|
|
|
|
| Option | Description |
|
|
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
| `autoPluralization` | Tell Mongoose to auto-pluralize any collection names if it encounters any singular words used as collection `slug`s. |
|
|
| `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](https://mongoosejs.com/docs/connections.html#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](https://www.mongodb.com/docs/manual/core/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](https://www.mongodb.com/docs/manual/reference/collation/). |
|
|
| `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. |
|
|
|
|
## 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
|
|
Limitations with [DocumentDB](https://aws.amazon.com/documentdb/) and [Azure Cosmos DB](https://azure.microsoft.com/en-us/products/cosmos-db):
|
|
|
|
* For Azure Cosmos DB you must pass `transactionOptions: false` to the adapter options. Azure Cosmos DB does not support transactions that update two and more documents in different collections, which is a common case when using Payload (via hooks).
|
|
* For Azure Cosmos DB the root config property `indexSortableFields` must be set to `true`.
|
|
* The [Join Field](../fields/join) is not supported in DocumentDB and Azure Cosmos DB, as we internally use MongoDB aggregations to query data for that field, which are limited there. This can be changed in the future.
|
|
* For DocumentDB pass `disableIndexHints: true` to disable hinting to the DB to use `id` as index which can cause problems with DocumentDB. |