【问题标题】:Pull one element out of all the duplicates in an mongodb array从 mongodb 数组中的所有重复项中拉出一个元素
【发布时间】:2021-05-16 19:16:33
【问题描述】:

这是我在 MongoDB 中的数组:

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

我只想拉取和删除数组中的“4444”个元素中的 1 个,但是当我尝试时它会拉出所有这些元素。

我能做些什么来删除其中一个重复的吗?

【问题讨论】:

    标签: node.js mongodb mongoose discord.js


    【解决方案1】:

    你可以使用这种方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-19
      • 2021-10-10
      • 2013-11-25
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多