I have a collection that has objects of various random structures/depths. Within them, there is one object that I want to get as my results, which has a specific key/value pair that I can use to find them.
An example from my collection:
{
"_id": ObjectId("123"),
"someKey": "blue",
"children": [
{
"foo": "bar",
"anotherKey": "anotherValue",
"whateverA": 111
}
]
}
{
"_id": ObjectId("456"),
"blahKey": "dog",
"children": [
{
"anotherRandom": "randomValue",
"children": [
{
"moreRandom": "stuffValue",
"children": [
{
"foo": "bar",
"animalKey": "legsValue",
"whateverB": 222
}
]
}
]
}
]
}
I would like to search for subdocuments that contain "foo: bar", and get a result that looks like the following:
{
"foo": "bar",
"anotherKey": "anotherValue",
"whateverA": 111
}
{
"foo": "bar",
"animalKey": "legsValue",
"whateverB": 222
}
Then I can paginate the results. Is this even possible in MongoDB 5?
Thank you.