Check the record already exists in MongoDB?

Hi, I am using n8n to migrate data from MySQL to MongoDB. Before inserting the data into MongoDB I want to check the record already exists in MongDB.

Is there any way to check the record already exists in MongoDB?

Hey @mkiran!

You can use the Find operation in the MongoDB node. In the Query field you can enter your MongoDB query to search for the records.

yes @harshil1712, I am doing like this only, but how can I check the mongoDB query returned the results or not?

In the Settings tab of the node, set Always Output Data to true. You can then connect the IF node to the MongoDB node to check if the MongoDB node returned any value or not.

hey @harshil1712

Could you please provide me a sample code for this.

hey @harshil1712 I got it. Thanks for the help.

1 Like

hey @harshil1712
Now am in different problem. MySQL query is returning more than 100 results. And when I run the workflow it is inserted first record only.

When I run it again it is not inserting any rows. Please help me to fix this issue also

Hey @mkiran,

n8n handles your data (100+ records) and insert all the data that got returned to MongoDB. Can you check your MongoDB database and see whether all the data got added or not? Also, it might help if you can share your workflow :slightly_smiling_face:

@harshil1712, no it is not inserting all the records verified in MongoDB also. Following is the workflow

Can you share the workflow code? Select all the nodes (Ctrl + A or CMD + A), copy them using CTRL + C, and paste it here.

This might be helpful: https://docs.n8n.io/reference/contributing.html#help-out-the-community-🤝

hi @harshil1712 here is the workflow code

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -40,
        20
      ],
      "disabled": true
    },
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        200,
        90
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "select username, realname, email from user_table "
      },
      "name": "MySQL",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 1,
      "position": [
        340,
        90
      ],
      "credentials": {
        "mySql": "MySQL-DB"
      }
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "User",
        "fields": "=firstName, lastName, email, userName, isLDAPUser, password"
      },
      "name": "Mongo-Insert",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        1000,
        90
      ],
      "credentials": {
        "mongoDb": "mongo-db"
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "firstName",
              "value": "={{$node[\"MySQL\"].json[\"realname\"]}}"
            },
            {
              "name": "lastName",
              "value": "={{$node[\"MySQL\"].json[\"realname\"]}}"
            },
            {
              "name": "email",
              "value": "={{$node[\"MySQL\"].json[\"email\"]}}"
            },
            {
              "name": "password",
              "value": "$2a$10$svZKT.0pDKy138QCmvv82udg9qpeJHX2ntHeUuX5s6tS.L7OI9dJO"
            },
            {
              "name": "userName",
              "value": "={{$node[\"MySQL\"].json[\"username\"]}}"
            }
          ],
          "boolean": [
            {
              "name": "isLDAPUser"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        840,
        100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"MySQL\"].json[\"username\"]}}",
              "value2": "={{$node[\"MongoDB - Find\"].json[\"userName\"]}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        100
      ]
    },
    {
      "parameters": {
        "collection": "User",
        "query": "={\"userName\":\"{{$node[\"MySQL\"].json[\"username\"]}}\"}"
      },
      "name": "MongoDB - Find",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        500,
        100
      ],
      "alwaysOutputData": true,
      "credentials": {
        "mongoDb": "mongo-db"
      }
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "MySQL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MySQL": {
      "main": [
        [
          {
            "node": "MongoDB - Find",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Mongo-Insert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        null,
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB - Find": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @harshil1712
FYI, If I remove already exists (MongoDB-Find) check, then all the records are inserting.

hey @harshil1712

I found the solution here Loop through MySQL Results