如何删除MongoDB集合中的多个对象/字段

vlju58qv  于 2023-04-29  发布在  Go
关注(0)|答案(1)|浏览(132)

我试图删除存储在mongoDB文档中的部分数据(见下面的示例),我想删除所有“任务变量”(它不是一个数组)的数据,一些文档包含数千个。
数据:

{
    "Id": "1",
    "referenceId": "A001",
    "createdAt": "2023-04-25T11:08:06.384Z",
    "data": {
        "errors": {
            "archive": {
                "data": {
                    "A1": {
                        "name": "Error",
                        "stamp": "2023-04-25T11:08:10.700Z",
                        "task-variables": {
                            "taskErrorMessage": null,
                            "nrOfActiveInstances": 1,
                            "errorCode": "error-archiving"
                        }
                    },
                    "A2": {
                        "name": "Error",
                        "stamp": "2023-04-25T11:08:10.700Z",
                        "task-variables": {
                            "taskErrorMessage": null,
                            "nrOfActiveInstances": 1,
                            "errorCode": "error-archiving"
                        }
                    }
                }
            }
        }
    }
}

我尝试了下面的例子,但它不起作用。如何删除/删除它?

db.getCollection("my-collection").updateOne(
   { referenceId: "A001" },
   { $unset: { "data.errors.archive.data.$**.task-variables": "" } }
)
t9aqgxwy

t9aqgxwy1#

db.getCollection("my-collection").updateOne(
  { referenceId: "A001" },
  { $unset: { "data.errors.archive.data.A1.task-variables": "", "data.errors.archive.data.A2.task-variables": "" } }
)

相关问题