【问题标题】:mongodb deleteOne for email in document arraymongodb deleteOne 用于文档数组中的电子邮件
【发布时间】:2023-03-26 11:44:01
【问题描述】:

我在一个集合中有以下文档模型:

{
   "user":{
      "name":string,
      "email":string
   },
   "addres":{
      "direction":string,
      "country":string
   },
   "id":string
}

如果要按id删除文档,没问题:

dataUser.deleteOne( {id: id});

如果我通过电子邮件尝试,它不会删除它:

dataUser.deleteOne( {"user": {email: email}})

我尝试了其他命令,但它也没有删除它。

非常感谢。

【问题讨论】:

    标签: javascript node.js mongodb mongodb-query


    【解决方案1】:

    您必须使用dot notation 来访问对象属性。

    检查this 示例如何除了this one 之外没有找到任何文档。

    所以你必须使用:

    dataUser.deleteOne( {"user.email": email})
    

    【讨论】:

    • 谢谢 J. F. ;)
    • 没问题!此外,如果问题已解决,请将答案标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 2019-12-07
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多