1

这是我的结构:

{
    "_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"}})

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

4

2 回答 2

2

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

您应该将查询更改为:

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

正如@BryceAtNetwork23 建议的那样。

于 2014-01-31T20:07:07.490 回答
1

尝试这个:

db.slides.update( {"components._id": ObjectId("52ebf4d971616b8716000011")},
{$pull: {components: {"_id": ObjectId("52ebf4d971616b8716000011")}}}, false, false)
于 2014-01-31T20:03:31.520 回答