gpt4 book ai didi

javascript - 在嵌套子文档数组中查找与特定条件匹配的文档总数

转载 作者:行者123 更新时间:2023-11-30 20:14:39 27 4
gpt4 key购买 nike

我有一组文档,其中包含来自不同博彩公司的比赛。

 [
{
"_id": "5b83e6340818897dc988f57d",
"pseudoKey": "hannover-dortmund",
"sport": "Soccer",
"league": "Bundesliga",
"date": "2018-08-31T18:30:00.000Z",
"team1": "Hannover",
"team2": "Dortmund",
"matchInstances": [
{
"team1": {
"name": "Hannover",
"price": "5.41"
},
"team2": {
"name": "Dortmund",
"price": "1.57"
},
"pseudoKey": "hannover-dortmund",
"service": "Sportpesa",
"region": "Kenya",
"drawPrice": "4.44",
"url": "https://www.sportpesa.co.ke/games/1427914/markets?league=76390&top=1&sid=3679&sportId=1"
},
{
"team1": {
"name": "Hannover",
"price": "5.65"
},
"team2": {
"name": "Dortmund",
"price": "1.53"
},
"pseudoKey": "hannover-dortmund",
"service": "Betika",
"region": "Kenya",
"drawPrice": "4.58",
"url": "https://www.betika.com/mobile/dist/#/prebets/markets/542303"
},
{
"team1": {
"name": "Hannover",
"price": "6"
},
"team2": {
"name": "Dortmund",
"price": "1.57"
},
"pseudoKey": "hannover-dortmund",
"service": "1XBet",
"region": "Kenya",
"drawPrice": "4.8",
"url": "https://1xbet.co.ke/en/line/Football/96463-Germany-Bundesliga/28884025-Hannover-96-Borussia-Dortmund/"
},
{
"team1": {
"name": "Hannover",
"price": "5.75"
},
"team2": {
"name": "Dortmund",
"price": "1.49"
},
"pseudoKey": "hannover-dortmund",
"service": "Betpawa",
"region": "Kenya",
"drawPrice": "4.85",
"url": "https://www.betpawa.co.ke/event-458477-hannover-96-borussia-dortmund"
}
],
"__v": 0
},
{
"_id": "5b83e6340818897dc988f578",
"pseudoKey": "nurnberg-mainz",
"sport": "Soccer",
"league": "Bundesliga",
"date": "2018-09-01T13:30:00.000Z",
"team1": "Nurnberg",
"team2": "Mainz",
"matchInstances": [
{
"team1": {
"name": "Nurnberg",
"price": "2.42"
},
"team2": {
"name": "Mainz",
"price": "3.18"
},
"pseudoKey": "nurnberg-mainz",
"service": "Sportpesa",
"region": "Kenya",
"drawPrice": "3.12",
"url": "https://www.sportpesa.co.ke/games/1428636/markets?league=76390&top=1&sid=1835&sportId=1"
},
{
"team1": {
"name": "Nurnberg",
"price": "2.39"
},
"team2": {
"name": "Mainz",
"price": "3.19"
},
"pseudoKey": "nurnberg-mainz",
"service": "Betika",
"region": "Kenya",
"drawPrice": "3.15",
"url": "https://www.betika.com/mobile/dist/#/prebets/markets/542710"
},
{
"team1": {
"name": "Nurnberg",
"price": "2.49"
},
"team2": {
"name": "Mainz",
"price": "3.38"
},
"pseudoKey": "nurnberg-mainz",
"service": "1XBet",
"region": "Kenya",
"drawPrice": "3.18",
"url": "https://1xbet.co.ke/en/line/Football/96463-Germany-Bundesliga/28235773-1.-Nurnberg-1.-FSV-Mainz-05/"
},
{
"team1": {
"name": "Nurnberg",
"price": "2.30"
},
"team2": {
"name": "Mainz",
"price": "3.15"
},
"pseudoKey": "nurnberg-mainz",
"service": "Betpawa",
"region": "Kenya",
"drawPrice": "3.30",
"url": "https://www.betpawa.co.ke/event-458949-nurnberg-mainz-05"
}
],
"__v": 0
},
{
"_id": "5b83e6340818897dc988f583",
"pseudoKey": "leverkusen-wolfsburg",
"sport": "Soccer",
"league": "Bundesliga",
"date": "2018-09-01T13:30:00.000Z",
"team1": "Leverkusen",
"team2": "Wolfsburg",
"matchInstances": [
{
"team1": {
"name": "Leverkusen",
"price": "1.68"
},
"team2": {
"name": "Wolfsburg",
"price": "4.81"
},
"pseudoKey": "leverkusen-wolfsburg",
"service": "Sportpesa",
"region": "Kenya",
"drawPrice": "4.11",
"url": "https://www.sportpesa.co.ke/games/1428638/markets?league=76390&top=1&sid=1155&sportId=1"
},
{
"team1": {
"name": "Leverkusen",
"price": "1.63"
},
"team2": {
"name": "Wolfsburg",
"price": "4.99"
},
"pseudoKey": "leverkusen-wolfsburg",
"service": "Betika",
"region": "Kenya",
"drawPrice": "4.22",
"url": "https://www.betika.com/mobile/dist/#/prebets/markets/542711"
},
{
"team1": {
"name": "Leverkusen",
"price": "1.68"
},
"team2": {
"name": "Wolfsburg",
"price": "5.4"
},
"pseudoKey": "leverkusen-wolfsburg",
"service": "1XBet",
"region": "Kenya",
"drawPrice": "4.3",
"url": "https://1xbet.co.ke/en/line/Football/96463-Germany-Bundesliga/28235769-Bayer-04-Leverkusen-VfL-Wolfsburg/"
},
{
"team1": {
"name": "Leverkusen",
"price": "1.62"
},
"team2": {
"name": "Wolfsburg",
"price": "4.85"
},
"pseudoKey": "leverkusen-wolfsburg",
"service": "Betpawa",
"region": "Kenya",
"drawPrice": "4.40",
"url": "https://www.betpawa.co.ke/event-458936-bayer-leverkusen-wolfsburg"
}
],
"__v": 0
},
{
"_id" : ObjectId("5b7689bc6c947619191879d7"),
"pseudoKey" : "nimes-marseille",
"sport" : "Soccer",
"league" : "Ligue 1",
"date" : ISODate("2018-08-19T19:00:00.000Z"),
"team1" : "Nimes",
"team2" : "Marseille",
"matchInstances" : [
{
"team1" : {
"name" : "Nimes",
"price" : "4.45"
},
"team2" : {
"name" : "Marseille",
"price" : "1.74"
},
"pseudoKey" : "nimes-marseille",
"service" : "Betin",
"region" : "Kenya",
"drawPrice" : "4.00",
"url" : "https://web.betin.co.ke/Sport/SubEventOdds.aspx?SubEventID=18398860"
}
],
"__v" : 0
}

/* 29 */
{
"_id" : ObjectId("5b7689bc6c947619191879d8"),
"pseudoKey" : "nantes-caen",
"sport" : "Soccer",
"league" : "Ligue 1",
"date" : ISODate("2018-08-25T18:00:00.000Z"),
"team1" : "Nantes",
"team2" : "Caen",
"matchInstances" : [
{
"team1" : {
"name" : "Nantes",
"price" : "1.85"
},
"team2" : {
"name" : "Caen",
"price" : "4.65"
},
"pseudoKey" : "nantes-caen",
"service" : "Betin",
"region" : "Kenya",
"drawPrice" : "3.40",
"url" : "https://web.betin.co.ke/Sport/SubEventOdds.aspx?SubEventID=18431759"
}
],
"__v" : 0
}

/* 30 */
{
"_id" : ObjectId("5b7689bc6c947619191879d9"),
"pseudoKey" : "marseille-rennes",
"sport" : "Soccer",
"league" : "Ligue 1",
"date" : ISODate("2018-08-26T19:00:00.000Z"),
"team1" : "Marseille",
"team2" : "Rennes",
"matchInstances" : [
{
"team1" : {
"name" : "Marseille",
"price" : "1.65"
},
"team2" : {
"name" : "Rennes",
"price" : "5.30"
},
"pseudoKey" : "marseille-rennes",
"service" : "Betin",
"region" : "Kenya",
"drawPrice" : "3.90",
"url" : "https://web.betin.co.ke/Sport/SubEventOdds.aspx?SubEventID=18431757"
}
],
"__v" : 0
}
...
]

我想获得德甲比赛的总数。我使用了聚合管道 $unwind$group 阶段

db.getCollection('matches').aggregate({ $unwind : "$matchInstances" },
{ $group: {
_id: '',
count: { $sum: 1 }
}
})

我正在获取所有联赛中所有比赛的总数。

{
"_id" : "",
"count" : 373.0
}

感谢任何帮助。

最佳答案

只需将 $match stage 添加为第一阶段,即可在展开前按联赛过滤文档。

db.getCollection('matches').aggregate(
{$match:{"league":"Bundesliga"}},
{ $unwind : "$matchInstances" },
{ $group: {
_id: '',
count: { $sum: 1 }
}
})

关于javascript - 在嵌套子文档数组中查找与特定条件匹配的文档总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com