I'm trying to obtain 3 subdocuments from my collection using aggregation framework (I'd like to use only aggregation because this is part of a long pipeline).
I know only one property of one subdocument. I need to retrieve that subdoc and the previous and next one too.
Example:
[
{
"title": "Title 34",
"subDoc": {
"subtitle": "subtitle 8",
"order": 8
}
},
{
"title": "Title 34",
"subDoc": {
"subtitle": "subtitle 6",
"order": 6
}
},
{
"title": "Title 34",
"subDoc": {
"subtitle": "subtitle 11",
"order": 11
}
},
{
"title": "Title 34",
"subDoc": {
"subtitle": "subtitle 13",
"order": 13
}
},
{
"title": "Title 34",
"subDoc": {
"subtitle": "subtitle 7",
"order": 7
}
}
]
I knew only one subtitle order (for example, "11"), and I need to obtain something like this:
[
{
"title": "Title 34",
"subDoc": [
{
"subtitle": "subtitle 8",
"order": 8
},
{
"subtitle": "subtitle 11",
"order": 11
},
{
"subtitle": "subtitle 13",
"order": 13
}
]
}
]
I've tried with unwind, order, limit, but I can get only the previous OR the next document, not both.
Playground here: https://mongoplayground.net/p/0228GD-b6oZ