【问题标题】:Find document, then find entry within array and remove it from the array查找文档,然后在数组中查找条目并将其从数组中删除
【发布时间】:2017-06-30 11:15:07
【问题描述】:

在 Node 和 Mongoose 中,我想从文档的数组中删除一个对象。结构是这样的。

{ 
    _id: ObjectId,
    title: String,
    tags: [
        { text: String },
        { text: String }
    ]
}

我将通过 _id 查找一个项目,但随后我想在标签中查找某个 String 并将其从数组中删除。

【问题讨论】:

  • 然后呢?你被困在哪里了?你试过什么?

标签: javascript node.js mongodb mongoose mongodb-query


【解决方案1】:

您实际上想要update 文档并将$pull 运算符与匹配"tags.tag" 下的匹配值的查询一起使用:

Model.update(
    { "_id": docId, "tags.tag": "mytag" },
    { "$pull": { "tags": { "tag": "mytag" } },
    function(err,numAffected) {

    }
)

【讨论】:

    猜你喜欢
    • 2018-08-22
    • 2015-07-03
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多