Passing arrays to MongoDB delete

Hi all, despite reading through the doco this forum, I can’t work out how iteration works for arrays in n8n.

I’m try to pass an array to a mongoDB delete node. I have tried several data structures but it only seems to delete the first value (of several) each time to the workflow runs. What am I missing?

Below is an example of the array I’m passing to the mongoDB delete node:

[
  {
    "todoist_task_id": 5454090595
  },
  {
    "todoist_task_id": 5454090628
  },
  {
    "todoist_task_id": 5454090670
  }
]

Please share the workflow

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "collection": "test_col",
        "options": {},
        "query": "{\"source\":\"google_mail\"}"
      },
      "name": "MongoDB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        240,
        600
      ],
      "credentials": {
        "mongoDb": {
          "id": "13",
          "name": "MongoDB account"
        }
      }
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "integration_id",
              "value": "={{$json[\"_id\"]}}"
            }
          ],
          "number": [
            {
              "name": "todoist_task_id",
              "value": "={{$json[\"todoist_task_id\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        420,
        600
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "operation": "get",
        "taskId": "={{$json[\"todoist_task_id\"]}}"
      },
      "name": "Todoist",
      "type": "n8n-nodes-base.todoist",
      "typeVersion": 1,
      "position": [
        580,
        600
      ],
      "credentials": {
        "todoistOAuth2Api": {
          "id": "11",
          "name": "Personal - Todoist account"
        }
      }
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [],
          "boolean": [],
          "number": [
            {
              "name": "todoist_task_id",
              "value": "={{$json[\"id\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        740,
        600
      ]
    },
    {
      "parameters": {
        "operation": "delete",
        "collection": "test_col",
        "query": "={\"todoist_task_id\": {{$node[\"Set1\"].json[\"todoist_task_id\"]}}}"
      },
      "name": "MongoDB1",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        980,
        600
      ],
      "credentials": {
        "mongoDb": {
          "id": "13",
          "name": "MongoDB account"
        }
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "MongoDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Todoist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Todoist": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "MongoDB1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Share the output returned by the last node

[
  {
    "deletedCount": 1
  }
]

Yes, there are sadly a few nodes in n8n right now that behave not the way they should. The MongoDB Node is one of them. Instead of executing once per item, do they only execute once. So what you want to do is to iterate over them 1 by 1 with a SplitInBatches Node which is set to Batch Size 1.

For example like this:

{
  "nodes": [
    {
      "parameters": {
        "collection": "test_col",
        "options": {},
        "query": "{\"source\":\"google_mail\"}"
      },
      "name": "MongoDB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        480,
        300
      ],
      "credentials": {
        "mongoDb": {
          "id": "13",
          "name": "MongoDB account"
        }
      }
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "integration_id",
              "value": "={{$json[\"_id\"]}}"
            }
          ],
          "number": [
            {
              "name": "todoist_task_id",
              "value": "={{$json[\"todoist_task_id\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "operation": "get",
        "taskId": "={{$json[\"todoist_task_id\"]}}"
      },
      "name": "Todoist",
      "type": "n8n-nodes-base.todoist",
      "typeVersion": 1,
      "position": [
        820,
        300
      ],
      "credentials": {
        "todoistOAuth2Api": {
          "id": "11",
          "name": "Personal - Todoist account"
        }
      }
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [],
          "boolean": [],
          "number": [
            {
              "name": "todoist_task_id",
              "value": "={{$json[\"id\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        980,
        300
      ]
    },
    {
      "parameters": {
        "operation": "delete",
        "collection": "test_col",
        "query": "={\"todoist_task_id\": {{$node[\"Set1\"].json[\"todoist_task_id\"]}}}"
      },
      "name": "MongoDB1",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        1400,
        300
      ],
      "credentials": {
        "mongoDb": {
          "id": "13",
          "name": "MongoDB account"
        }
      }
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ]
    }
  ],
  "connections": {
    "MongoDB": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Todoist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Todoist": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB1": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "MongoDB1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I tried your suggestion but it still didn’t quite achieve the result I hoped for! It seemed to split the batch and pass the individual items to the mongoDB delete not but it only deleted the first item.

I think I worked it out!! I wasn’t using the JSON output of the split batch for the mongoDB delete.