I have a Jar
collection, and its documents hold red and blue marbles. If I wanted to get the top 5 Jars on the basis of having a lot of red marbles, I would do something like this:
Jar.find({}).sort({red: "desc"}).limit(5).exec((err, results) => { ... });
But what if I wanted to get the top 5 Jars on the basis of having a lot of red OR blue marbles, without duplicates? Is there a way to take the results of two different sorts in this fashion?
So for an example, if I have
[{red: 20, blue: 10}, {red: 10, blue: 3}, {red: 5, blue: 22}, {red: 10, blue: 10}]
, the top 3 (in descending order) will be
[{red: 5, blue: 22}, // 22 from blue is largest
{red: 20, blue: 10}, // 20 from red is 2nd largest
{red: 10, blue: 10}] // 10 from red (or 10 from blue) is 3rd largest
You can do this by using a few aggregation stages, consider the following:
db.collection.aggregate([
{
"$addFields": {
maxMarbleCount: {
$max: [
"$red",
"$blue"
]
}
}
},
{
"$sort": {
maxMarbleCount: -1
}
},
{
"$limit": 3
}
])
Basically I add a temporary field called maxMarbelCount
to each document, which is determined by the max red/blue value. You can then ( desc
)-sort by this field and limit accordingly.
If needed, you can add another projection
stage to remove the temporary field from the output.
Here's an example on mongoplayground: https://mongoplayground.net/p/QApaz7YKWy8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.