Cannot use `ObjectId()`s in `findRaw`
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 47.6k
- Forks
- 2.5k
- Avg merge
- 21h 59m
- Merged PRs (30d)
- 95
Description
Bug description
With a fresh install of Prisma, and a simple schema, I cannot use the mongodb ObjectId constructor in any of the raw query methods. No data is returned when using them.
If I turn on profiling in the mongodb shell (using db.setProfilingLevel(2)) then run the script below, it outputs with this (keys omitted for brevity):
{
"op" : "query",
"ns" : "prisma-mongodb-run-command.User",
"command" : {
"find" : "User",
"filter" : {
"_id" : "649d848c1355833f9323a566"
},
"$db" : "prisma-mongodb-run-command"
},
"nreturned" : 0
}
If I run the runCommand in the mongo shell directly, like so:
db.runCommand({ find: 'User', filter: { _id: new ObjectId("649d848c1355833f9323a566") }})
And check the profiler again:
{
"op" : "query",
"ns" : "prisma-mongodb-run-command.User",
"command" : {
"find" : "User",
"filter" : {
"_id" : ObjectId("649d848c1355833f9323a566")
},
"$db" : "prisma-mongodb-run-command"
},
"nreturned" : 1
}
Note that command.filter._id is correctly wrapped in an ObjectId constructor and is not just a String type. I hope I'm just doing something silly here...
How to reproduce
- Upsert a User with a fixed email address to guarantee that user exists (schema included further down)
- Perform a regular
findMany()query to return it and get the id value - Perform a
findRaw(), passing in the previous id as an ObjectId so that mongodb can recognise it - Observer that no data is returned for the query
Expected behavior
The data should be returned for that user. I've also observed similar behaviour for $runCommandRaw when trying to issue a delete by _id as well, so I suspect the same issue here is present there too.
Prisma information
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
}
import { PrismaClient } from '@prisma/client'
import { ObjectId } from 'mongodb'
const prisma = new PrismaClient()
async function main() {
const user = { email: 'test@example.com' };
await prisma.user.upsert({ where: user, create: user, update: user });
const foundUser = await prisma.user.findMany();
console.log(foundUser[0]);
console.log(await prisma.user.findRaw({ filter: { _id: new ObjectId(foundUser[0].id) }}))
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
Output when running this:
{ id: '649d848c1355833f9323a566', email: 'test@example.com' }
[]
Environment & setup
- OS: macOS 13.3.1
- Database: MongoDB
- Node.js version: v16.20.0
Prisma Version
Environment variables loaded from .env
prisma : 4.16.1
@prisma/client : 4.16.1
Current platform : darwin-arm64
Query Engine (Node-API) : libquery-engine b20ead4d3ab9e78ac112966e242ded703f4a052c (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine : migration-engine-cli b20ead4d3ab9e78ac112966e242ded703f4a052c (at node_modules/@prisma/engines/migration-engine-darwin-arm64)
Format Wasm : @prisma/prisma-fmt-wasm 4.16.0-66.b20ead4d3ab9e78ac112966e242ded703f4a052c
Default Engines Hash : b20ead4d3ab9e78ac112966e242ded703f4a052c
Studio : 0.484.0
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.
Research direction
Start with the MongoDB raw query methods, especially findRaw and $runCommandRaw, and reproduce the supplied script against MongoDB. Trace how the ObjectId filter is represented in the raw command; done means the profiler shows an ObjectId rather than a string and the query returns the existing user.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, nodejs, typescript
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100