payloadcms / payloadcms/payload

Payload MongoDB Adapter: LocalAPI returns ids as ObjectIDs when installing mongodb

Open
#11,703 12 comments 5 reactions 1 assignee View on GitHub

@r1tsuu is already working on this.

Since May 12, 2025.

Dominant language
TypeScript
Stars
44.8k
Forks
4.2k
Avg merge
2d 21h
Merged PRs (30d)
53

Description

Describe the Bug

Usually Payload's LocalAPI will return ids (either the document's id or a field of type relationship) as strings when fetching data.
This behaviour breaks when installing mongodb as a project dependency. Now all ids are returned as ObjectIds. This leads to two issues:

  1. React will complain when passing props containing ObjectIds from Server to Client Components:
Only plain objects can be passed to Client Components from Server Components. Objects with toJSON methods are not supported. Convert it manually to a simple value before passing it to props.
  1. When fetching a document via LocalAPI and then using the document's id to update another document's relationship field, the LocalAPI throws an error, because it expects a string.

This issue must've surfaced recently, it used to work as expected in earlier versions (we upgraded from 3.14.0 to 3.28.1).

Link to the code that reproduces this issue

https://github.com/dietmarweiss/payload-object-id-issue.git

Reproduction Steps
  1. run pnpm i
  2. rename .env.example to .env
  3. run pnpm dev
  4. visit http://localhost:3000
  5. Check the Browser console, you will see the React error about serialization
  6. Check the Terminal logs and you will see that the logged id is of type ObjectId, not string

When you uninstall mongodb from the project and restart the App you will see that the id is now a string and the React error is gone.

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

db-mongodb

Environment Info
Node.js v22.6.0

Binaries:
  Node: 22.6.0
  npm: 10.8.2
  Yarn: N/A
  pnpm: 9.7.1
Relevant Packages:
  payload: 3.28.1
  next: 15.2.2
  @payloadcms/db-mongodb: 3.28.1
  @payloadcms/next/utilities: 3.28.1
  @payloadcms/payload-cloud: 3.28.1
  @payloadcms/richtext-lexical: 3.28.1
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: darwin
  Arch: arm64

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.