Možná budete chtít použít $cond operátor, který to zpracuje:
db.items.aggregate([
{"$project":
{
"name": "$name",
"upvotes": "$upvotes",
"downvotes": "$downvotes",
"quality": { $cond: [ { $eq: [ "$downvotes", 0 ] }, "N/A", {"$divide":["$upvotes", "$downvotes"]} ] }
}
},
{"$sort": {"quality":-1}}
]);