0

I have a calls collection which documents are like:

{
    _id: 'abcdefg',
    timestamp: 1510328686448
}

I want to get, based on a call id, the previous and the following call.

I know that this can be easily done with two queries but I have to limit the latency.

Can any one guide me to build the correct aggregation pipeline to achieve this?

Zied Feki
  • 812
  • 1
  • 10
  • 19
  • I would try `$graphLookup` out of curiousity but you could also try group by call id and then do some kind of sort + filtered unwind. – Daniel W. Nov 10 '17 at 15:56
  • There really is no "current way" with the aggregation framework or any other process in a "single request" to do this. From MongoDB 3.6, it would be "possible" using the [extended syntax on `$lookup`](https://docs.mongodb.com/master/reference/operator/aggregation/lookup/#uncorrelated-subquery) for a "Non-correlated Sub-query" to the current item. But it still would likely not be a performance improvement over parallel execution of "previous" and "next" queries subsequent to the result. In short, no operator exists to do this in any efficient manner either now or in near future. – Neil Lunn Nov 11 '17 at 02:52

0 Answers0