Here is the document sample:
{
"_id": "658016",
"playerName": "Kuldeep Singh",
"tournamentId": "197831",
"score": [
{
"_id": "1611380",
"runsScored": 21,
"ballFaced": 10,
"fours": 3,
"sixes": 1,
"strikeRate": 210,
"oversBowled": 3,
"runsConceded": 34,
"economyRate": 11.33,
"wickets": 0,
"maiden": 0,
"howToOut": "c Mayank b Nitin Gudle -(reddy)",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1602721",
"runsScored": 21,
"ballFaced": 14,
"fours": 1,
"sixes": 1,
"strikeRate": 150,
"oversBowled": 3,
"runsConceded": 28,
"economyRate": 9.33,
"wickets": 0,
"maiden": 0,
"howToOut": "not out",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1536540",
"runsScored": 1,
"ballFaced": 3,
"fours": 0,
"sixes": 0,
"strikeRate": 33.33,
"oversBowled": 2,
"runsConceded": 7,
"economyRate": 3.5,
"wickets": 3,
"maiden": 0,
"howToOut": "b Lalit",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1536483",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1501201",
"runsScored": 12,
"ballFaced": 5,
"fours": 1,
"sixes": 1,
"strikeRate": 240,
"oversBowled": 2,
"runsConceded": 28,
"economyRate": 14,
"wickets": 0,
"maiden": 0,
"howToOut": "c Rahul Mogri b Harshal Tupe",
"catches": [
"Chetan Tupe",
"Kaustubh Tupe"
],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1501195",
"runsScored": 74,
"ballFaced": 27,
"fours": 5,
"sixes": 7,
"strikeRate": 274.07,
"oversBowled": 3,
"runsConceded": 44,
"economyRate": 14.67,
"wickets": 0,
"maiden": 0,
"howToOut": "not out",
"catches": [
"Ashish Thantharatey"
],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1461428",
"runsScored": 8,
"ballFaced": 4,
"fours": 0,
"sixes": 1,
"strikeRate": 200,
"oversBowled": 3,
"runsConceded": 29,
"economyRate": 9.67,
"wickets": 2,
"maiden": 0,
"howToOut": "run out Mayank / Sahil Raheja",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1461413",
"runsScored": 23,
"ballFaced": 12,
"fours": 0,
"sixes": 3,
"strikeRate": 191.67,
"oversBowled": 2,
"runsConceded": 18,
"economyRate": 9,
"wickets": 0,
"maiden": 0,
"howToOut": "c Sumit Gandhi b Arun Stambhampelli",
"catches": [
"Arun Stambhampelli (c)"
],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1451183",
"runsScored": 16,
"ballFaced": 6,
"fours": 2,
"sixes": 1,
"strikeRate": 266.67,
"oversBowled": 2,
"runsConceded": 11,
"economyRate": 5.5,
"wickets": 0,
"maiden": 0,
"howToOut": "c Syed Jamaal Nasir b Aman Deep",
"catches": [
"Mohan Slathia (c)"
],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1451132",
"runsScored": 26,
"ballFaced": 10,
"fours": 1,
"sixes": 3,
"strikeRate": 260,
"oversBowled": 3,
"runsConceded": 18,
"economyRate": 6,
"wickets": 3,
"maiden": 0,
"howToOut": "c Javed b Ram Lomte",
"catches": [
"Saheb Desai"
],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1392788",
"runsScored": 13,
"ballFaced": 6,
"fours": 2,
"sixes": 0,
"strikeRate": 216.67,
"oversBowled": 3,
"runsConceded": 30,
"economyRate": 10,
"wickets": 0,
"maiden": 0,
"howToOut": "not out",
"catches": [
"Sachin Patil"
],
"stumping": [],
"runout": [],
"participatedRunout": [
"Pankaj Rastogi"
]
},
{
"_id": "1611380",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1602721",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1536540",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1501201",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1501195",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1461428",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1461413",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1451183",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1451132",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
},
{
"_id": "1392788",
"runsScored": 0,
"ballFaced": 0,
"fours": 0,
"sixes": 0,
"strikeRate": 0,
"oversBowled": 0,
"runsConceded": 0,
"economyRate": 0,
"wickets": 0,
"maiden": 0,
"howToOut": "-",
"catches": [],
"stumping": [],
"runout": [],
"participatedRunout": []
}
],
"__v": 0
}
Note: in score array inside this document, there is duplication of objects after index 10.
The documents are initially updated (Before this, score array is empty)using this code:
await Scorecard.findOneAndUpdate(
{ _id: fielder.player_id, },
{
$addToSet: {
"score": {
_id: matchID
}
}
}
);
Later in code, objects inside score array are updated again to change default values(set in mongoose schema) of initally unchanged fields.
But, the problem is when the above quoted code runs again after sometime, it duplicates all the objects inside array. I know, there are other similar questions, but I am beginner in mongodb and not able to make complex aggregation pipelines. Any help is greatly appreciated.
matchID_id. It looks to me that some other content may need to supplied to replicate your issue.