MongoDB connections for Read/Write operation

Hello all,

I have started using mongodb node recently and I have some queries regarding the connections.

  • How does connections work in mongodb?
  • For every read/write operation a new connection used?
  • After every operation does the connection get closed or I can re-use it for next operation?
  • If I am making multiple request to mongodb (sequential or parallel) does it create a new connection for every request or I can res-use it?

Hi Ali, when quickly testing a workflow like below, only one connection appears to have been used as per mongodb.com’s monitoring:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "default",
        "fields": "number",
        "options": {}
      },
      "name": "MongoDB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        680,
        300
      ],
      "credentials": {
        "mongoDb": {
          "id": "59",
          "name": "cluster0.0qyog.mongodb.net"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "return [...Array(10000).keys()].map(e => { return {\n  json: {\n      number: e\n    }\n  }\n});\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "MongoDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I am not sure how the node (and the library it’s using) would manage connections behind the scenes though, so I’ve asked the team for help on this one.

Hi @Mustafa_Ali, I can now confirm connections are not being re-used by n8n, instead a new connection is created for each operation.