我有一个对象 player_of_day 的数组,其中还有一组用户ID,即 nominate_by 我想找到一个具有最大计数的对象。 就像是- 来自 player_of_day 的对象,其计数为最大值
"player_of_day" : [
{
"nominate_by" : [
ObjectId("58994bca4187b412aec50c94"),
ObjectId("58994a124187b412aec50c91"),
ObjectId("589955d54187b412aec50c99")
],
"nominate_to" : ObjectId("58994b374187b412aec50c93"),
"count" : 3
},
{
"nominate_by" : [
ObjectId("58994c254187b412aec50c95"),
ObjectId("58994ad04187b412aec50c92")
],
"nominate_to" : ObjectId("58994a124187b412aec50c91"),
"count" : 2
},
{
"nominate_by" : [
ObjectId("58994b374187b412aec50c93")
],
"nominate_to" : ObjectId("58994c254187b412aec50c95"),
"count" : 1
}
],
答案 0 :(得分:0)
您可以使用$unwind
db.match.aggregate([{"$unwind":"$player_of_day"},{"$sort":{'player_of_day.count':-1}},{"$limit":1}]);