I’ve been struggling on finding a clean way to map an array to a new array that illustrates the number of times any element within that array immediately precedes another.
For instance, say this is the original array:
{
_id: objectId
"myArray": Array
0: Object
name: "A"
1: Object
name: "B"
2: Object
name: "A"
3: Object
name: "B"
}
The desired outcome would be something like:
{
_id: objectId
"precedeCount": Array
0: Object
first: "A"
second: "B"
count: 2
1: Object
first: "B"
second: "A"
count: 1
}
I’m wondering if this is possible with the MongoDB aggregation pipelines, or if I should just go back to MapReduce.
Thanks in advance.