如果嵌套数组在MongoDB中为空,如何删除主数组内的嵌套数组并删除整个数组?

时间:2018-08-18 08:33:24

标签: json mongodb web

我正在尝试删除MongoDB数组内的嵌套数组。现在,我可以从嵌套数组中删除单个数组,但是当嵌套数组完全为空时,我想删除主数组本身。

下面是我的JSON结构

1
    id : "5b76ae3a40c1c449d6409fa3"
    vendor_user_id : "5b17b992c440782b5a218cd2"
    product_type_id : "5ae8348b7ae0d9538e45ab46"
    condition_id
          0
            _id : "5ae977da7ff1706f3b7dc47a"
            status : 0
            date_added : "2018-08-17-11-15-06"    
          1
            _id : "5ae978187ff1706f3b7dc47c"
            status : 0
          date_added : "2018-08-17-11-15-06"
    shipping_cost : 100

现在,如果我删除($ pull)condition_id的所有嵌套元素,我希望主数组也被删除。

Ps:到目前为止,我可以删除单个元素,但是我不能删除删除嵌套元素,然后检查它是否为空以及是否不删除整个数组这两个任务

编辑1: 这是我的主要阵列。我想根据其ID删除数组condition_id内的内容,并且当condition_id为空时,我想删除存在它们的相应数组

  "product": [
    {
      "id": "5b71310eeff5c42f00f8a4e8",
      "vendor_user_id": "5b17b992c440782b5a218cd2",
      "product_type_id": "5ae834807ae0d9538e45ab45",
      "condition_id": [
        {
          "_id": "5ae977da7ff1706f3b7dc47a",
          "status": 1,
          "date_added": "2018-08-10-09-55-54"
        }
      ],
      "shipping_cost": "100",
      "date_added": "2018-08-10-09-55-54",
      "date_status_change": "2018-08-10-09-55-54",
      "status": 1
    },
    {
      "id": "5b76ae3a40c1c449d6409fa3",
      "vendor_user_id": "5b17b992c440782b5a218cd2",
      "product_type_id": "5ae8348b7ae0d9538e45ab46",
      "condition_id": [
        {
          "_id": "5ae977da7ff1706f3b7dc47a",
          "status": 0,
          "date_added": "2018-08-17-11-15-06"
        },
        {
          "_id": "5ae978187ff1706f3b7dc47c",
          "status": 0,
          "date_added": "2018-08-17-11-15-06"
        }
      ],
      "shipping_cost": 100,
      "date_added": "2018-08-17-11-15-06",
      "date_status_change": "2018-08-17-11-15-06",
      "status": 0
    },
    {
      "id": "5b76b41240c1c44d4d6e38f2",
      "vendor_user_id": "5b17b992c440782b5a218cd2",
      "product_type_id": "5ae834a67ae0d9538e45ab47",
      "condition_id": [
        {
          "_id": "5ae978587ff1706f3b7dc47e",
          "status": 0,
          "date_added": "2018-08-17-11-40-02"
        }
      ],
      "shipping_cost": 100,
      "date_added": "2018-08-17-11-40-02",
      "date_status_change": "2018-08-17-11-40-02",
      "status": 0
    }]

0 个答案:

没有答案