I've got a collection of items with the following structure (heavily simplified):
{
_id: 1,
transfers: [
{
date: 2017,
person: 'C'
},
{
date: 2012,
person: 'B'
},
{
date: 2010,
person: 'A'
}
]
}
I would like to query it by date to find out which person 'owned' the item at a given point in time. I have implemented this already in a javascript app by simply pulling the whole transfers array and iterating over it until
searchDate > transfers[i].date
For example, searching for a date of 2014 would return person B
I feel like it must be possible to achieve this logic in the Mongo query alone and reduce the amount of data I have to send to my application (transfers array can grow very large). I have tried to understand other solutions posted on here but I'm not sure I've found any that specifically apply to my problem and I lack the experience with writing mongo queries to adapt any similar to my needs.
Any help would be much appreciated.
Cheers,
P