ClaudiuFlorea's avatar

query array of object ids in MongoDB

i am using MongoDB.

In my database i have documents like

{ "_id" : ObjectId("6722ab32eaea7deb7de17098"), "unique_key" : "da3e7fc2eca33e62a4dc7112c480aaf5aa49ab2f4caf33f68f72af27e6d8af2b", "parent_category" : { "_id" : ObjectId("673da582e4c5d1cea407f218"), "name" : "Tricouri copii", "fullParents" : [ ObjectId("673da582e4c5d1cea407f218"), ObjectId("673da4b5e4c5d1cea407f217"), ObjectId("6728d2b04ea9ed9d5e0d3a7d") ], "updated_at" : "2024-11-20T09:01:54.210000Z", "created_at" : "2024-11-20T09:01:54.210000Z" } }.

How do i get all documents with laravel that parent_category.fullParents contain ObjectId("673da582e4c5d1cea407f218").

i thought that MyModel::where("parent_category.fullParents",ObjectId("673da582e4c5d1cea407f218") ) should work. it doesn't.

can anyone help me with this?

0 likes
4 replies
ClaudiuFlorea's avatar
ClaudiuFlorea
OP
Best Answer
Level 1

My bad,

IDE included for ObjectId - use MongoDB\Laravel\Eloquent\Casts\ObjectId; which is wrong. i added use MongoDB\BSON\ObjectId; and it worked.

Please or to participate in this conversation.