Return limited number of records of a certain type, but unlimited number of other records?

前端 未结 4 1316
清酒与你
清酒与你 2020-12-20 02:16

I have a query where I need to return 10 of \"Type A\" records, while returning all other records. How can I accomplish this?

Update: Admittedly, I

4条回答
  •  被撕碎了的回忆
    2020-12-20 02:44

    This is a classic case for subquery/join which is not supported by MongoDB. All joins and subquery-like operations need to be implemented in the application logic. So multiple queries is your best bet. Performance of the multiple query approach should be good if you have an index on type.

    Alternatively you can write a single aggregation query minus the type-matching and limit clauses and then process the stream in your application logic to limit documents per type. This approach will be low on performance for large result sets because documents may be returned in random order. Your limiting logic will then need to traverse to the entire result set.

提交回复
热议问题