How to use MongoDB aggregation query to obtain range of documents based on filtered status?
Consider the following example data:
[
{
"visible_id": 1,
"status": "INITIAL",
"name": "ab"
},
{
"visible_id": 2,
"status": "INITIAL",
"name": "aab"
},
{
"visible_id": 3,
"status": "SCANNED",
"name": "bba"
},
{
"visible_id": 4,
"status": "SCANNED",
"name": "cca"
},
{
"visible_id": 5,
"status": "INITIAL",
"name": "dds"
},
{
"visible_id": 6,
"status": "INITIAL",
"name": "aax"
},
{
"visible_id": 7,
"status": "INITIAL",
"name": "bsd"
},
{
"visible_id": 8,
"status": "PRINTED",
"name": "ads"
}
]
The aggregated query should list me all the available sticker in the filter status in a range value of visible_id. If a different status is found in between, a new range must be projected from the next filter status onward appended to the result list.
Example Filter and Results according to the above CSV data: if filter status = "INITIAL" result:
[
{status: "INITIAL", range: "1-2"},
{status: "INITIAL", range: "5-7"}
]
if filter status = "SCANNED" result:
[
{status: "SCANNED", range: "3-4"},
]
if filter status = "PRINTED" result:
[
{status: "PRINTED", range: "8-8"},
]