【问题标题】:I need to delete a subdocument in MongoDB我需要删除 MongoDB 中的子文档
【发布时间】:2014-02-24 15:55:34
【问题描述】:

这是我的结构:

{
    "_id" : ObjectId("52ebf3ba71616b871600000c"),
    "components" : [
        {
            "type" : "text",
            "text_type" : "subtitle",
            "pos_x" : 198.384521484375,
            "pos_y" : 114.43489074707031,
            "content" : "New subtitle",
            "font" : "",
            "font_size" : "",
            "color" : "",
            "bold" : false,
            "italic" : false,
            "underlined" : false,
            "rotation" : 0,
            "scale" : 0,
            "custom_css" : "",
            "_id" : ObjectId("52ebf3c171616b871600000d")
        },
        {
            "type" : "text",
            "text_type" : "title",
            "pos_x" : 198.384521484375,
            "pos_y" : 114.43489074707031,
            "content" : "New title",
            "font" : "",
            "font_size" : "",
            "color" : "",
            "bold" : false,
            "italic" : false,
            "underlined" : false,
            "rotation" : 0,
            "scale" : 0,
            "custom_css" : "",
            "_id" : ObjectId("52ebf3c371616b871600000e")
        },
        {
            "type" : "text",
            "text_type" : "title",
            "pos_x" : 279.32373046875,
            "pos_y" : 265.3794403076172,
            "content" : "New title",
            "font" : "",
            "font_size" : "",
            "color" : "",
            "bold" : false,
            "italic" : false,
            "underlined" : false,
            "rotation" : 0,
            "scale" : 0,
            "custom_css" : "",
            "_id" : ObjectId("52ebf44471616b871600000f")
        },
        {
            "type" : "text",
            "text_type" : "subtitle",
            "pos_x" : 55.32373046875,
            "pos_y" : 35.37944030761719,
            "content" : "New subtitle",
            "font" : "",
            "font_size" : "",
            "color" : "",
            "bold" : false,
            "italic" : false,
            "underlined" : false,
            "rotation" : 0,
            "scale" : 0,
            "custom_css" : "",
            "_id" : ObjectId("52ebf44571616b8716000010")
        },
        {
            "type" : "image",
            "file" : "",
            "external_url" : "",
            "size" : 40,
            "pos_x" : 0,
            "pos_y" : 0,
            "rotation" : 0,
            "scale" : 0,
            "custom_css" : "",
            "_id" : ObjectId("52ebf4d971616b8716000011")
        }
    ],
    "number" : 0,
    "pos_x" : 0,
    "pos_y" : 0,
    "presentation_id" : 46,
    "rotation_x" : 0,
    "rotation_y" : 0,
    "rotation_z" : 0,
    "scale" : 1
}

我需要删除一个子文档,在这种情况下,“组件”的 _id 等于“52ebf4d971616b8716000011”。

我已经试过了:

db.slides.update({"components._id": ObjectId("52ebf4d971616b8716000011")},
{$pull: {"components._id": ObjectId("52ebf4d971616b8716000011"}})

但它不起作用,文档保持不变。

【问题讨论】:

  • 算了。我问自己。问题出在 $pull 结构中: db.slides.update({"components._id": ObjectId("52ebf4d971616b8716000011")}, {$pull: {"components": { "_id": ObjectId("52ebf4d971616b8716000011") }}})
  • 你能发布你最终做了什么来解决它吗?

标签: mongodb document subdocument


【解决方案1】:

试试这个:

db.slides.update( {"components._id": ObjectId("52ebf4d971616b8716000011")},
{$pull: {components: {"_id": ObjectId("52ebf4d971616b8716000011")}}}, false, false)

【讨论】:

  • 我的支持,因为你是第一个正确回答的人。
【解决方案2】:

您尝试的更新不起作用,因为您试图从components._id(不是数组)中提取,而不是从基于_id(它的是您尝试提取文档的字段)。

您应该将查询更改为:

db.slides.update(
  { 'components._id': some-id }, 
  { $pull: { components: { _id: some-id } } }); 

正如@BryceAtNetwork23 建议的那样。

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2016-02-28
    • 2013-07-23
    相关资源
    最近更新 更多