2014년 12월 6일 토요일

how to find latest version document for each id in collection?

I have a collection with documents such as { id: 1, version: 1 }, { id: 1, version:2 }, { id: 2, version: 1 }. 
Is it possible to construct a query that gets me the document with greatest version for each id so that the query would return [{ id: 1, version: 2}, { id: 1, version: 1 }]?



I wrote a multi-part series on different ways to deal with this, including the answer to your question, you can read them here:


In a nutshell, when you're looking for a single id, latest version, it's a find with sort and limit, but if you are looking for multiple id's then you need to use aggregation pipeline to query.


댓글 없음:

댓글 쓰기