Skip to content

Mongo pipeline builder: match() equality on _id never matches — resolveValue walks bson ObjectId as a plain object #925

Description

@nurul3101

Package and version

@prisma-next/mongo-adapter / @prisma-next/mongo-query-builder @ 0.14.0 (repo main @ 284a838)

What happened?

A pipeline builder match() with an equality filter on _id never matches any document — it silently returns zero rows for both value forms:

  • passing the hex string (f._id.eq('665f0c…')) — this typechecks cleanly, making it a silent zero-rows trap;
  • passing a bson ObjectId instance.

The cause is in resolveValue() (packages/3-mongo-target/2-mongo-adapter/src/resolve-value.ts): after the MongoParamRef / null / Date / Array checks, any other object falls into the generic-object branch, which walks it with Object.entries() and rebuilds it as a plain object. A bson ObjectId therefore gets destructured into its internal buffer property and reaches the wire as a plain { buffer: … }-shaped document instead of an ObjectId, so the equality comparison against the stored ObjectId is never true. The hex-string form likewise never becomes an ObjectId on the encode path.

What did you expect to happen?

An _id equality filter matches the stored document: hex strings are encoded to ObjectId via the mongo/objectid@1 codec, and ObjectId instances pass through to the driver untouched (i.e. resolveValue needs a BSON-type guard alongside the existing Date guard).

Minimal reproduction

const inserted = await run(query.from('orders').insertOne({ status: 'active' }));
const id = inserted.insertedId; // hex string per the read surface

const rows = await run(
  query
    .from('orders')
    .match((f) => f._id.eq(id)) // typechecks; also fails with new ObjectId(id)
    .build(),
);
// rows === []  — expected the inserted document

Reproduced against mongodb-memory-server with a failing spec (specs/pipeline-builder.mongo.spec.ts in a local docs-validation harness); both the hex-string and ObjectId variants return [].

Environment

  • Node: 24.x
  • OS: macOS 15 (Darwin 24.6.0)
  • Package manager: pnpm
  • Database: MongoDB via mongodb-memory-server

Additional context

Related but distinct from #577, which is about the decode side (create() returning raw ObjectId instead of the hex string). This issue is the encode side: filter values are never turned into ObjectIds, so _id equality is unusable from the pipeline builder. Found while validating the examples in docs/reference/Mongo Pipeline Builder.md against the runtime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-triageAwaiting maintainer triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions