Basically, I have a nested array of objects. I want to extract information from those objects, and return all the results as an array.
Here is my data:
[
{
"_id": "608642db80a36336946620aa",
"title": "titleHere",
"types": {
"flashcardReversed": [
{
"normal": { // the data inside of "normal" should become its own object in the returned array
"_id": "608d5b290e635ece6828141X",
"front": "2front",
"back": "2back"
},
"reversed": { // the data inside of "reversed" should become its own object in the returned array
"_id": "608t5b290e635ece6828141Y",
"front": "2frontReversed",
"back": "2backReversed"
}
},
{
"normal": {
"_id": "608a5b31a3f9806de253726X",
"front": "2front2",
"back": "2back2"
},
"reversed": {
"_id": "608a5b31a3f9806de253726Y",
"front": "2frontReversed2",
"back": "2backReversed2"
}
}
]
}
}
]
Here is the desired result:
[
{
"flashcardReversed": [
{
"_id": "608d5b290e635ece6828141X",
"front": "2front",
"back": "2back"
},
{
"_id": "608t5b290e635ece6828141Y",
"front": "2frontReversed",
"back": "2backReversed"
},
{
"_id": "608t5b290e635ece6828141Y",
"front": "2frontReversed",
"back": "2backReversed"
},
{
"_id": "608a5b31a3f9806de253726Y",
"front": "2frontReversed2",
"back": "2backReversed2"
}
]
}
]
My current attempt: https://mongoplayground.net/p/k3Fjd59KMKg
I have tried merging the $card and $card2 documents through various methods, and then using $replaceRoot to create the desired result - but after much time trying, I was unsuccessful.
Some notes about my use-case -
I will be using this pipeline inside $facet
There will be many objects inside of flashcardReversed, all created from the same Mongoose Schema
The document should have a max of ~2000 cards. I need the results for use in a react website. Should I be worried about the efficiency of this search? (if this should be a question in itself I would be happy to move it).
If my Mongoose Schemas would be helpful, let me know and I can add them.
All help is appreciated,
- Riley Swinson