MongoDB findRaw filter problem with ObjectId field.
See original GitHub issueBug 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:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @casperibo, since ids are represented with
ObjectId
s or$oid
on the db level, you need to build your query like the following: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.