Skip to content
Advertisement

how to sort result of lookup in mongodb

I want to join two collections and sort the date field in the foreign table and then have group the fields to get only one record from recent date for each device as below

JavaScript

By using the above code in php I got the below result, joining two collections is working fine but sorting and grouping is not working.

JavaScript

I need result as below

JavaScript

Advertisement

Answer

You may need a $unwind, so you can perform grouping on looked up collection. Add this line after $match.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement