Java / MongoDB – query child documents based off field that is only present in parent?

  Kiến thức lập trình

In my mongo collection I have a documents that follow a parent-child structure.

Each parent doc typically has 4 fields, with the children having 3 (no group field).

parent:

{
_id: 123
parent_id: 123
active: true
group: A
}

children

{
id: 123_1
parent_id: 123
active: true
}

{
id: 123_2
parent_id: 123
active: true
}

I want to write bson query / aggreation if needed for my java spring project that will return all the docs that match the following fields provided by user:

- active field - this will be true or false
- group field - e.g "A"

My difficulty is that each child document is assumed to have the same value as the parent for the group field, but it is not actually in the document.

How can I write a query that will match all the parent and child documents for a certain group?

LEAVE A COMMENT