Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB findRaw filter problem with ObjectId field. #11830

Closed
Tracked by #8788
casperibo opened this issue Feb 15, 2022 · 7 comments
Closed
Tracked by #8788

MongoDB findRaw filter problem with ObjectId field. #11830

casperibo opened this issue Feb 15, 2022 · 7 comments
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. tech/engines Issue for tech Engines. tech/typescript Issue for tech TypeScript. topic: mongodb topic: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli
Milestone

Comments

@casperibo
Copy link

Bug description

Hi, I try to get items by its categories using findRaw method but I couldn't.

I think it's a type problem, I should write like ObjectId("categoryId");

I installed mongodb and try to use;

const catId = new ObjectId(categoryId);

$categoryId: { $eq: catId }

But It didn't work.

const [totalCount, items] = await Promise.all([
          ctx.prisma.store.count({ where: { isPublished: 1, categoryId } }),
          ctx.prisma.store
            .findRaw({
              filter: {
                location: {
                  $nearSphere: {
                    $geometry: {
                      type: "Point",
                      coordinates: [currentLng, currentLat],
                    },
                  },
                },
                isPublished: { $eq: 1 },
                categoryId: { $eq: categoryId }, // After I add this line, I'm getting empty array even though the category has items.
              },

              options: {
                skip: offset,
                limit: first,
              },
            })
        ]);

How to reproduce

Expected behavior

Filter items which has defined categoryId.

Prisma information

Environment & setup

  • OS: Mac OS
  • Database: MongoDB
  • Node.js version: 16.2.0

Prisma Version

3.9.2
@casperibo casperibo added the kind/bug A reported bug. label Feb 15, 2022
@janpio janpio added domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: mongodb topic: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Feb 16, 2022
@matthewmueller
Copy link
Contributor

Thanks @casperibo, I've confirmed this bug.

Reproduction: https://github.com/matthewmueller/prisma-issue-11830

@matthewmueller matthewmueller added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Feb 16, 2022
@casperibo
Copy link
Author

Hi @matthewmueller thank you for your interest.

I tried a few things like you said but unfortunately it didn't work.

I try to filter from MongoDb Atlas filter area and I got the result that I want.

{_id: ObjectId('620a7a90dd71db222fb2f371')}

{categoryId: ObjectId('620a7a32fee6919239264eb0')}

The schema looks like same but I saw your repository that you share, and the version doesn't have same version like my project.

Is there any way you to try in version 3.9.2. or Is there something I can do.

My Schema fields:

model Store {
...
id          String        @id @default(dbgenerated()) @map("_id") @db.ObjectId
category    StoreCategory @relation(fields: [categoryId], references: [id])
categoryId  String        @db.ObjectId()
...
}
const test = await ctx.prisma.store.findRaw({
          // filter: {
          //   _id: { $eq: new ObjectId("620a7a90dd71db222fb2f371") },
          // } as any,
          // filter: {
          //   _id: { $eq: "620a7a90dd71db222fb2f371" },
          // } as any,
          // filter: {
          //   id: { $eq: new ObjectId("620a7a90dd71db222fb2f371") },
          // } as any,
          // filter: {
          //   id: { $eq: "620a7a90dd71db222fb2f371" },
          // } as any,
          // filter: {
          //   _categoryId: { $eq: new ObjectId("620a7a32fee6919239264eb0") },
          // } as any,
          // filter: {
          //   _categoryId: { $eq: "620a7a32fee6919239264eb0" },
          // } as any,
          // filter: {
          //   categoryId: { $eq: new ObjectId("620a7a32fee6919239264eb0") },
          // } as any,
          // filter: {
          //   categoryId: { $eq: "620a7a32fee6919239264eb0" },
          // } as any,
        });

        console.log(test);

@matthewmueller matthewmueller added tech/engines Issue for tech Engines. tech/typescript Issue for tech TypeScript. labels Feb 22, 2022
@millsp millsp self-assigned this Feb 22, 2022
@matthewmueller matthewmueller added this to the 3.11.0 milestone Feb 22, 2022
@millsp
Copy link
Member

millsp commented Feb 25, 2022

Hey @casperibo, since ids are represented with ObjectIds or $oid on the db level, you need to build your query like the following:

    const users = await prisma.user.findRaw({
        filter: {
            _id: { $eq: { $oid: "6202ec178be26d2b28adc075" } },
        },
    })

@casperibo
Copy link
Author

casperibo commented Feb 27, 2022

Hi @millsp It's helped a lot thank you. I solved my problem.

I would like to ask more question though. When I use findRaw method to get datas, the object ids coming like;

_id: { $oid: "id" };

And it's causes a problem on graphql like "non nullable field id not found" something like that.

Do you have any solution for that problem?

@millsp
Copy link
Member

millsp commented Feb 28, 2022

You're welcome :) Right now, I don't see any quick solution to that problem. The easiest would be for you to use middlewares to transform the returned objects to the shape you need https://www.prisma.io/docs/concepts/components/prisma-client/middleware.

@janpio
Copy link
Contributor

janpio commented Feb 28, 2022

(If you do, please share the resulting Middleware - others might benefit from it as well.)

@ghalibansari
Copy link

ghalibansari commented Jun 23, 2023

unable to do limit and skip in findRaw, not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. tech/engines Issue for tech Engines. tech/typescript Issue for tech TypeScript. topic: mongodb topic: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli
Projects
None yet
Development

No branches or pull requests

5 participants