[英]How to avoid duplication in array of objects in a mongodb document
這是文檔示例:
{
"_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
}
注意:在本文檔中的 score 數組中,索引 10 之后存在重復的對象。
文檔最初使用以下代碼更新(在此之前,分數數組為空):
await Scorecard.findOneAndUpdate(
{ _id: fielder.player_id, },
{
$addToSet: {
"score": {
_id: matchID
}
}
}
);
稍后在代碼中,再次更新 score 數組中的對象以更改最初未更改字段的默認值(在 mongoose 模式中設置)。
但是,問題是當上面引用的代碼在一段時間后再次運行時,它會復制數組中的所有對象。 我知道,還有其他類似的問題,但我是 mongodb 的初學者,無法制作復雜的聚合管道。 任何幫助是極大的贊賞。
$addToSet
運算符僅在數組不存在的情況下將元素添加到數組中。
該數組中的每個元素都是一個對象。 對象按照https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#objects 上的 mongodb 文檔中所述進行比較 基本上,如果兩個對象具有完全相同的字段,則它們是相等的,在完全相同的順序,每個順序都具有完全相同的值。 換句話說,如果它們不相同,則它們不相等。
我在問題的數組中沒有看到任何重復項。 有一些_id
字段具有相同的值,但那些包含其他幾個不同的字段,因此它們不是重復的。
如果您需要更新數組中對象的值,還有其他幾個關於修改嵌套文檔的問題。
我懷疑您要做的是檢測數組中是否已存在具有該 id 的對象,如果已預設則更新,如果沒有則插入。
在這種情況下,您可以嘗試使用更新的管道形式,以及filter
和mergeObjects
運算符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.