Hi, I’m trying to build a projection that can help me with versioning. I’ve taken guidance from Building with Patterns: The Polymorphic Pattern | MongoDB this leaves me with a document which has an array of objects, some of these objects may also be arrays. I’ve abstracted an example document below:
I’ve been trying so far (unsuccessfully with the arrayElemAt and the slice operators) I haven’t been able to test the $last operator as its not supported on my db ( I plan to upgrade).
Please share what you tried because intuitively it would be the way I would go.
I would have a $map on keyPoints that would check if I have an object or an array. In the case of an object the mapped value would be $$this and for array I the mapped value would be $slice with -1 position.
The document you have in your playground is different from the one you shared in your first post.
The one in your playground presents an easier problem since all elements of keyPoints in an array. In your original document keyPoints had a mix of objects and arrays confirmed by your screenshot.
With an array of arrays you do not have to
You simply
The above give the following trivial $map for your $project.