I have a database as below.
[
{
"title": "Data 1",
"slug": "data-1",
"sub_docs": [
{
"name": "Sub Doc 1",
"sub_slug": "sub-doc-1",
"urls": [
"https://example.com/path-1",
]
},
{
"name": "Sub Doc 2",
"sub_slug": "sub-doc-2",
"urls": [
"https://example.com/path-2"
]
},
{
"name": "Sub Doc 3",
"sub_slug": "sub-doc-3",
"urls": [
"https://example.com/path-3",
]
}
]
}
]
When I give data-1 and sub-doc-2 as input, I want to get the following result.
{
"title": "Data 1",
"sub_doc_title": "Sub Doc 2",
"urls": [
"https://example.com/path-2"
],
"prev": {
"name": "Sub Doc 1",
"sub_slug": "sub-doc-1"
},
"next": {
"name": "Sub Doc 3",
"sub_slug": "sub-doc-3"
}
}
I’m a bit of a beginner at MongoDB.
How can I do this query? Do you have an idea?
2
Answers
Thanks rickhg12hs for your answer. I also found a solution to this question.
Here is my answer;
That’s a tough query for beginner, although I still consider myself a novice so maybe there’s a better/easy way.
Here’s one way you could do it.
Try it on mongoplayground.net.