payloadcms / payloadcms/payload
Payload MongoDB Adapter: LocalAPI returns ids as ObjectIDs when installing mongodb
@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:
- 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.
- 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
- run
pnpm i - rename
.env.exampleto.env - run
pnpm dev - visit
http://localhost:3000 - Check the Browser console, you will see the React error about serialization
- Check the Terminal logs and you will see that the logged id is of type
ObjectId, notstring
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.