Skip to content

Global default values ignored when global document is missing in MongoDB #14476

@Fitch24

Description

@Fitch24

Describe the Bug

I’ve encountered an issue using globals with the MongoDB adapter.

When a global document does not exist in the globals collection, Payload returns an empty object from GET /api/globals/<global_slug>, and all default values defined in the global config are ignored.

Expected behavior:
If the global document does not yet exist in the database, the response should include the default values defined in the global config.

Actual behavior:
The response returns {}.

Example request:

GET /api/globals/<global_slug>

Actual response:

{}

Workaround:
Manually insert an empty document for the global in the globals collection:

{
  "_id": { "$oid": "<ObjectId>" },
  "globalType": "<global_slug>"
}

After doing this, the default values are applied as expected.

Notes:

  • I observed this with the MongoDB adapter. I haven’t tested with other database adapters.

Link to the code that reproduces this issue

https://github.com/Fitch24/payload/tree/reproduction/globals-defaults

Reproduction Steps

Clone reproduction repo and:

  1. pnpm install
  2. pnpm dev _community
  3. curl --location 'http://localhost:3000/api/globals/menu'

Expected response:

{
  "globalText": "This field must have default value if no \"global\" document exists in db"
}

Actual response:

{}

Which area(s) are affected? (Select all that apply)

db: mongodb

Environment Info

Binaries:
  Node: 22.20.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.62.1
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
  Available memory (MB): 24449
  Available CPU cores: 6

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs-triagePossible bug which hasn't been reproduced yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions