question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MongoDB findRaw filter problem with ObjectId field.

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
millspcommented, 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" } },
        },
    })
1reaction
millspcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query documents to filter out ObjectId - Working with Data
Is there a way to filter out the documents where the field is an ObjectId, so I only can see the cases where...
Read more >
MongoDB: filter (findOne/updateOne..) by ObjectId not working ...
You need to be able to spawn new ObjectIds, so enable that first: import { MongoClient, ObjectId } from "mongodb";. and filter like...
Read more >
prisma/prisma 3.11.0 on GitHub - NewReleases.io
In version 3.11.0 , we're adding the ability to filter embedded documents. ... MongoDB findRaw filter problem with ObjectId field.
Read more >
Cannot Use ObjectId to find one record in MongoDB : DBE-9704
Relates to 1 issue (0 unresolved). N. DBE-9613 MongoDB: JavaScript regular expression objects are not supported in the filter field.
Read more >
Filter by objectId field in Mongo
I'm trying to work out how to filter by an objectId field using the Mongo plugin. I've tried many combinations of = '{$oid:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found