我試圖找到一種方法來查找和刪除子文檔數組中相同字段的重複對象。如何在MongoDB中的子文檔數組中找到重複的字段
基本上我的收藏有這個模式。
[{
"_id": ObjectId("578953db976cea724bbdbb60"),
"units": [{
"description": "a1"
}, {
"description": "b1"
}, {
"description": "a1"
}, {
"description": "c1"
}]
}, {
"_id": ObjectId("578953db976cea724bbdbb61"),
"units": [{
"description": "a2"
}, {
"description": "b2"
}, {
"description": "a2"
}, {
"description": "c2"
}]
}]
我只是想找到並刪除重複字段與相同的描述。
所以對於第一,我需要確定分別爲 「A1」 和A2"
感謝
更新(輸出應該是):
[{
"_id": ObjectId("578953db976cea724bbdbb60"),
"units": [{
"description": "a1"
}, {
"description": "b1"
}, {
"description": "c1"
}]
}, {
"_id": ObjectId("578953db976cea724bbdbb61"),
"units": [{
"description": "a2"
}, {
"description": "b2"
}, {
"description": "c2"
}]
}]
有一定的模糊性,以你想要達到的目標。從文檔中刪除相同的對象時,應從中刪除哪個文檔?如果它們都來自兩者,那將很容易。 –
刪除重複項後的預期輸出是什麼? – styvane
@TanKimLoong是的我想從兩個數組中刪除重複的數據。 –