I have a MongoDB collection that looks something like this:
{
"name": "McAllister's Deli",
"menu": [
{"sandwich": 4},
{"spud": 3},
{"salad": 5},
{"cookie":2}
],
"reviews": 45
}
I would like to rank these restaurants based on the types of food they have and the number of reviews. For instance, if someone is looking for cookie
and sandwich
, McAllister's Deli
would return a ranking of say 19.28
by taking (cookie * sandwich * reviews) / menuItems
. Is there a way to optimize my query to take this ranking into account?
Edit: Since it was asked in a comment, I am currently using the Dart driver, but I am familiar with the Mongo shell and can translate a shell query to a query my driver understands.
db.rest.aggregate([{
$project: {
_id: 1,
sandwich: { $arrayElemAt: [ '$menu.sandwich', 0]},
cookie: { $arrayElemAt: [ '$menu.cookie', 0]},
menuItems: {$size: '$menu'}
}
},{
$project: {
_id: 1,
rank: { $divide: [{ $add: ['$sandwich', '$cookie']}, '$menuItems' ]}
}
}
])
probably can be combined. $project is separated for clearance.
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.