MongoDB query builder does not convert string IDs to ObjectId in .match() filters

Open
#30,782 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
mongodb, typescript
Domain
backend, databases

Research direction

Start by tracing how .match() builds and executes filters in the MongoDB query builder, then compare its handling of string IDs with the ORM filter path described in the issue. Use the reported transaction fixture or an equivalent test to check a hexadecimal string against an ObjectId field. Done when the query builder returns the matching document for the string filter, with regression coverage.

Written by the indexing model from the issue text.

Description

Package and version
  • Originally reported with @prisma/[email protected] and [email protected].
  • Reproduced against the local 8.0.0-rc.18 workspace, based on commit e18525ac2840668c77d2d0a8f27f5a282afc30b9. This verification did not use a fresh published-package installation.
What happened?

The MongoDB query builder returns no documents when filtering an ObjectId field with a valid hexadecimal string. The equivalent ORM filter finds the document.

Runtime verification confirmed that ORM string filters and query-builder filters using an explicit ObjectId work. Only the query-builder string filter returned no results.

What did you expect to happen?

A valid hexadecimal string accepted by the ORM's ObjectId filter also matches the corresponding document through the query builder, instead of silently returning no results.

Minimal reproduction
// use prisma-8
// contract.prisma — emit this contract and initialize the MongoDB database.
// An equivalent repository fixture was runtime-tested; this reduced example was not separately run.
model Transaction {
  id        ObjectId @id @default(auto()) @map("_id")
  accountId ObjectId

  @@map("transactions")
}
// In mongosh, seed the application's database with one matching document.
db.transactions.insertOne({
  _id: ObjectId("507f1f77bcf86cd799439012"),
  accountId: ObjectId("507f1f77bcf86cd799439011")
});
// Using the application's existing db instance:
const runtime = await db.runtime();
const accountId = '507f1f77bcf86cd799439011';
const ormRows = await db.orm.transactions.where({ accountId }).all(); // Finds the document.
const plan = db.query
  .from('transactions')
  .match((f) => f.accountId.eq(accountId))
  .build();
const rows = await runtime.query(plan); // Returns []; expected: the matching document.
Environment

Runtime verification:

  • Node.js: 26.4.0
  • Database: MongoDB 8.2.6, temporary instance managed by mongodb-memory-server
  • Prisma: local 8.0.0-rc.18 workspace

Original report:

  • Node.js: 24.9.0
  • OS: macOS, Darwin 24.6.0
  • Package manager: npm 11.6.0
  • Database: MongoDB 8.2.0-rc4, local single-node replica set
  • Scaffold: [email protected]
Additional context

Workarounds: use ORM .where({ accountId }), or pass new ObjectId(accountId) to the query-builder comparison.

Dominant language
TypeScript
Stars
47.8k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
227

Getting set up

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.

More from prisma/orm

All issues in prisma/orm

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.