简体   繁体   English

从 mongodb 阵列中的所有重复项中拉出一个元素

[英]Pull one element out of all the duplicates in an mongodb array

大批

This is my array in MongoDB:这是我在 MongoDB 中的数组:

cardOwners: Array cardOwners:数组
0: "9999" 0:“9999”
1: "4444" 1:“4444”
2: "4444" 2:“4444”
3: "4444" 3:“4444”

I want to ONLY pull and remove 1 of the "4444" elements in the array, but it pulls away all of them when I try it out.我只想拉出并删除数组中的 1 个“4444”元素,但是当我尝试时它会拉出所有这些元素。

Is there anything I can do to just remove 1 of the duplicate ones?我能做些什么来删除其中一个重复的吗?

you can use this approach你可以使用这种方法

let result = await card.findOne({filter })
const index = result.cardOwners.findIndex(element => element === "4444");
result.cardOwners.splice(index, 1);
await result.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM