按ID删除MongoDB子文档

时间:2019-09-06 04:14:20

标签: node.js database mongodb nosql

编辑:

这是我的外壳中数据的样子:

{
    "_id" : ObjectId("5d6f3b29782ef1988ccd94f3"),
    "data" : {
        "fara" : {
            "names" : [ ],
            "enabled" : false,
            "tags" : [ ]
        },
        "senators" : {
            "names" : [
                "5d64511970ee667cc6fdd264",
                "5d64511970ee667cc6fdd265",
                "5d64511970ee667cc6fdd269",
                "5d64511970ee667cc6fdd263"
            ],
            "enabled" : false,
            "tags" : [
                {
                    "_id" : ObjectId("5d706e3fe1bf18b4eb5385db"),
                    "row" : "5d64511970ee667cc6fdd263",
                    "data" : "hi"
                },
                {
                    "_id" : ObjectId("5d706f20550b97b588c78f31"),
                    "row" : "5d64511970ee667cc6fdd263",
                    "data" : "hello"
                }
                ....

我正在尝试通过id删除文档内列出的子文档,换句话说,删除标签子文档之一(例如说“ hello”的子文档)。子文档在数据结构的“标签”字段中列出。如果重要的话,我正在用Node.js编写。

猫鼬模式如下:

const userSchema = new Schema({
    data: {
      senators: {
        enabled: {
          type: Boolean
        },
        names: {
          type: Array
        },
        tags: [{
          id: {
            type: String
          },
          row: {
            type: String
          },
          data: {
            type: String
          }
        }]
      },
      senateCandidates: {
        enabled: {
          type: Boolean
        },
        names: {
          type: Array
        },
        tags: [{
          id: {
            type: String
          },
          row: {
            type: String
          },
          data: {
            type: String
          }
        }]
      }
    }
});

我正在尝试根据ID删除“ senators.tag”数组中的单个子文档。我的路线如下所示:

deleteSenatorTag: async (req, res) => {
    try {
        await User.findOneAndUpdate({ "_id": req.params.id }, { /// $pull using req.param.tid? /// })
        res.status(200).send(); 
    } catch(err){
        res.status(500).send({
            message: err.message || 'This tag could not be deleted.'
        })
    }
}

我要传递的参数包括:

1)整个文档的ID,以req.param.id的形式提供给路线

2)我要删除的子文档的ID,以req.param.tid提供

这两个都是简单的字符串。

有什么方法可以运行一个简单的更新功能,以根据第二个参数删除子文档?我假设它可能是$ pull运算符,但是我还无法弄清楚该怎么做。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

如果您正在使用 Mongoose 客户端,并想通过_id搜索/拉动元素,则需要将“ _id” String 转换为 Mongo ObjectId。

从您的数据中,我了解到您正在按_id搜索文档,并希望从具有匹配标签_id的标签数组中提取对象。

var mongoose = require("mongoose");

await User.findOneAndUpdate(
    { "_id":  mongoose.Types.ObjectId(req.params.id) },
    {
        $pull: {
            "data.senators.tags": {
                _id: mongoose.Types.ObjectId(req.param.tid)
            }
        }
    }
)

以上语法将起作用,您可以根据需要在上述语法中修改标签搜索条件。

答案 1 :(得分:0)

尝试

await User.findOneAndUpdate(
    { "_id": req.params.id },
    {
        $pull: {
            data: {
               senators: {
                   tags: {
                       id: req.param.tid
                   }
               }
            }
        }
    }
)