[Question] How to where other field of collection #1989
Answered
by
zigzagdev
letuananh1873
asked this question in
General
-
|
I have a collection is manga (_id, title, slug) and chapter At Chapter Model Result show OK, but I want to where field slug of manga collection. How to do it? Thank you so much! |
Beta Was this translation helpful? Give feedback.
Answered by
zigzagdev
Dec 23, 2025
Replies: 1 comment
-
|
Hi. You can’t filter by manga.slug directly until after the $lookup, because slug lives in the joined documents (story). Also, your $lookup likely should join chapters.manga to manga._id (not id), unless you truly have an id field in manga. Here is a correct approach. $collection = self::raw(function ($collection) use ($manga_slug, $chapter_slug) {
return $collection->aggregate([
['$match' => ['name' => $chapter_slug]],
['$lookup' => [
'from' => 'manga',
'localField' => 'manga',
'foreignField' => '_id',
'as' => 'story',
]],
['$unwind' => '$story'],
['$match' => ['story.slug' => $manga_slug]],
]);
});Hope this helps. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
GromNaN
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@letuananh1873
Hi.
You can’t filter by manga.slug directly until after the $lookup, because slug lives in the joined documents (story). Also, your $lookup likely should join chapters.manga to manga._id (not id), unless you truly have an id field in manga.
Here is a correct approach.