MongoDB: How to “union all” results from same collection?
问题 query.1: db.test.find({category_id:1}).sort({createAt:-1}).limit(5); query.2: db.test.find({category_id:2}).sort({createAt:-1}).limit(5); What I want is use one query get results of query1+query2, and then sort the results by createAt . 回答1: You can use $facet aggregation here. db.test.aggregate([ { "$facet": { "first": [ { "$match": { "category_id": 1 }}, { "$sort": { "createAt": -1 }}, { "$limit": 5 } ], "second": [ { "$match": { "category_id": 2 }}, { "$sort": { "createAt": -1 }}, { "