Mongodb query

Hi

I am trying to use the mongodb query via the mondo node.

I have a property called “event.topic”: “radar_in_use”

In N8N (no results)

In 3t studio (1 result)

I have tried any thing i could i think about and could not find any documentation.

Kind regards
Mattias

Hm do currently not understand why it should not work. From what you described I guess your document looks something like this:

{
  "event" : {
    "topic" : "radar_in_use"
  },
 "something": "else"
}

So I created the following workflow which creates this document and then queries it directly. which worked fine for me. Can you please check if that is also the case for you.

{
  "nodes": [
    {
      "parameters": {
        "collection": "mctags",
        "query": "{ \"event.topic\": \"radar_in_use\" }"
      },
      "name": "MongoDB2",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        1040,
        300
      ],
      "credentials": {
        "mongoDb": ""
      }
    },
    {
      "parameters": {
        "functionCode": "items[0].json = { \"event\": {\"topic\": \"radar_in_use\" }, \"something\": \"else\" };\nreturn items;\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "mctags",
        "fields": "={{Object.keys($node[\"Function1\"].json).join(',')}}"
      },
      "name": "MongoDB3",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        840,
        300
      ],
      "credentials": {
        "mongoDb": ""
      }
    }
  ],
  "connections": {
    "Function1": {
      "main": [
        [
          {
            "node": "MongoDB3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB3": {
      "main": [
        [
          {
            "node": "MongoDB2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

For some reason did it work after i droped the collection in morgon and then created a new, but before that it did not work. Strange

Still thanks for the assistance.

That is very strange. Wonder what was going on there!

Anyway, for now happy to hear that it works.

Hi again!
I dont know if should create a new topic of it is fine to ask it here.

Everything with the look up is working fine but still my plan failed. I have talked with our devs aswell but could not find a solution.

I would like to store a mapping to an item in previous node inside in the json file (in mongo) Set pictures below. The stored mapping should then be used to get the data from a previous node. I know it might be a bit strange explanation but i hope the pictures explains it better.

We have tried JSON.parse but did not work .

Data from mongo
mongojsonnode

How it looks when i map it into the httprequest

How i would like it to work (This i achieved by manually copying the json into the node)

One again thanks for a great software:)

So if I understand it correctly do you have an n8n expression saved in your database and you want to then evaluate it after you read if from there? If that is the case, then it is currently sadly not possible. We would have to create and expose an additional function to do that. I added it to the To-Do list.

Ok thank you! Then it was not us:) Yeah if you be great to be able to parse the json back then i think it would work:)

Then i need to find a work around in the in the meantime:)

Ok that is now possible with the just released [email protected]

In expressions $evaluateExpression() and in Function-Nodes evaluateExpression() can be used.

The documentation about it can be found here:
https://docs.n8n.io/#/nodes?id=method-evaluateexpressionexpression-string-itemindex-number

An example workflow which uses all the different possibilities:

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "string1a",
              "value": "test"
            },
            {
              "name": "string1b",
              "value": "{{$node[\"Set\"].json[\"string1a\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "string2a",
              "value": "={{$node[\"Set\"].json[\"string1a\"]}}"
            },
            {
              "name": "string2b",
              "value": "={{$evaluateExpression($node[\"Set\"].json[\"string1b\"])}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json.string3a = evaluateExpression($node[\"Set\"].json[\"string1b\"], 0);\nitems[0].json.string3b = evaluateExpression('{{1+2}}', 0);\n\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.string4a = evaluateExpression($node[\"Set\"].json[\"string1b\"]);\nitem.string4b = evaluateExpression('{{1+2}}');\nreturn item;"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks! Will try it directly! Thanks again for an amazing software, we will do anything we can to support it.

We tried it out but it is not really solve thing problem yet

We would like following

//This evaluation is working 
//"shipId" : "{{$node['Webhook'].json['body']['Location']['GlobalId']}}", 
items[0].json.string3a = evaluateExpression($node["MongoDB"].json["event"]["location"]["shipId"], 0);
-------------
But we would need
// We would like to evaluate the whole json. Json will include the expression as value of some properties  
items[0].json.string3a = evaluateExpression($node["MongoDB"].json, 0);

return items;

Ah yes, that would not work. Giving the whole objects is something very unique and a custom need and so should probably not be in n8n itself. At least do not expect that a lot of people would need that.
But that is not a problem. With the evaluateExpression function you can now solve that yourself. You can simply write your own custom code in a Function-Node which recursively goes through all the properties and then resolve the ones which, for example, start with “{”.

Okey thanks! I put one of the developers into it:)

Ah you can not yourself? Wait I have short look.

Well i guess i cant, still to much beginner on js;) You dont need to do it, Jens is already on it! I guess he will ask you if he don´t manage:)

Maybe could be good with some help, we got abit stucked with n8n nodes so an exmaple would be rreally helpful;)

The code is for sure not perfect but should cover the most cases:

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "string1a",
              "value": "test"
            },
            {
              "name": "string1b",
              "value": "{{$node[\"Set\"].json[\"string1a\"]}}"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "\nfunction resolveData(data) {\n  if (data !== null && typeof data === 'object') {\n    const returnData = {};\n    for (const key of Object.keys(data)) {\n      returnData[key] = resolveData(data[key]);\n    }\n    return returnData;\n  }\n\n  if (typeof data === 'string' && data.charAt(0) === '{') {\n    return evaluateExpression(data);\n  } \n  \n  return data;\n}\n\nreturn [\n  {\n    json: resolveData(items[0].json),\n  }\n]\n\n"
      },
      "name": "Convert",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      a1: \"a\",\n      a2: \"{{$node[\\\"Set\\\"].json[\\\"string1a\\\"]}}\",\n      a3: {\n        b1: 1,\n        b2: \"{{$node[\\\"Set\\\"].json[\\\"string1a\\\"]}}\",\n        b3: {\n          c1: null,\n          c2: \"{{$node[\\\"Set\\\"].json[\\\"string1a\\\"]}}\",\n        }\n      }\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "Mock Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock Data": {
      "main": [
        [
          {
            "node": "Convert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

THank you!

Works great! Will try to get better at this :slight_smile:

1 Like

Sorry for a long runner.

So now it works close to perfect. There is just one error we do not get why it occurs and we have tried everything.

When we have the json in function nodes it works perfectly

But when we place it with the real mongo DB then we get below error. The strange thing is when i then push only execute the failed node again then it works, but never on the first run.

ERROR: ‘getOwnPropertyDescriptor’ on proxy: trap reported non-configurability for property ‘0’ which is either non-existent or configurable in the proxy target

TypeError: 'getOwnPropertyDescriptor' on proxy: trap reported non-configurability for property '0' which is either non-existent or configurable in the proxy target
    at Function.keys (<anonymous>)
    at resolveData (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:5:30)
    at resolveData (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:6:25)
    at resolveData (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:6:25)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:20:11
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:24:2)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1043:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:67:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:557:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:370:62

Node with functions that works

{
  "nodes": [
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "http://n8n:5678/webhook-test/55/webhook/1e9ad27c-6d0d-11ea-bc55-0242ac130003",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={{$node[\"Convert\"].json[\"event\"][\"location\"][\"shipId\"]}}\n\n{{$node[\"Convert1\"].json[\"event\"][\"location\"][\"shipId\"]}}"
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1170,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "const item= [\n    {\n        json: {\n      \n\"_id\": \"5e7394da86ab4d55b48934bb\",\n \"event\" : {\n        \"eventUuId\" : \"\", \n        \"topicDiscipline\" : \"Navigation\", \n        \"topicCategory\" : \"equipment_status\", \n        \"topic\" : \"radar_in_use\", \n        \"localId\" : \"maranics/checklist/arrival_process/radar_in_use\", \n        \"namingRule\" : \"\", \n        \"universalId\" : \"\", \n        \"eventTimeStamp\" : \"\", \n        \"eventTimeZone\" : \"+1\", \n        \"eventGeoTimeZone\" : \"+1\", \n        \"eventAction\" : \"\", \n        \"sourceType\" : \"\", \n        \"sourceName\" : \"maranics/checklist\", \n        \"sourceDataType\" : \"ManualInput\", \n        \"mcSourceId\" : \"recWm53qlufEfLsAXrecwCzZFAtSfOp9gq\", \n        \"Version\" : \"0\", \n        \"Rev\" : \"1\", \n        \"location\" : {\n            \"shipId\" : \"{{$node['Webhook'].json['body']['Location']['GlobalId']}}\", \n            \"officeId\" : \"\", \n            \"mcLocationKey\" : \"\", \n            \"voyageId\" : \"\", \n            \"operationalMode\" : \"\", \n            \"incidentMode\" : \"\"\n        }\n    }, \n    \"data\" : {\n        \"equipmentObjects\" : {\n            \"standard\" : \"VIS\", \n            \"class\" : \"Navigation\", \n            \"id\" : \"723.11\", \n            \"state\" : \"running\", \n            \"configurationId\" : \"\", \n            \"value\" : \"\"\n        }\n    }\n        }\n    }\n];\n\nreturn item;\n"
      },
      "name": "MongoDB",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        530,
        450
      ]
    },
    {
      "parameters": {
        "functionCode": "\nfunction resolveData(data) {\n  if (data !== null && typeof data === 'object') {\n    const returnData = {};\n    for (const key of Object.keys(data)) {\n      returnData[key] = resolveData(data[key]);\n    }\n    return returnData;\n  }\n\n  if (typeof data === 'string' && data.charAt(0) === '{') {\n    return evaluateExpression(data);\n  } \n  \n  return data;\n}\n\nreturn [\n  {\n    json: resolveData($node[\"MongoDB\"].json),\n  }\n]\n\n"
      },
      "name": "Convert1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        860,
        440
      ]
    }
  ],
  "connections": {
    "MongoDB": {
      "main": [
        [
          {
            "node": "Convert1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert1": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The only thing I can think of right now is that the ID makes problems. The ID of Mongo documents is its own type and at the first run it is probably of that special type (and makes problems) and on the second run it got already converted to JSON and therefore it works.

You could try this node. It skips the “_id” property and makes instead sure that it gets directly converted to a “proper” string.

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "\nfunction resolveData(data) {\n  if (data !== null && typeof data === 'object') {\n    const returnData = {};\n    for (const key of Object.keys(data)) {\n    console.log('key: ' + key);\n      if (key === '_id') {\n        returnData[key] = data[key].toString();\n        continue;\n      }\n      returnData[key] = resolveData(data[key]);\n    }\n    return returnData;\n  }\n\n  if (typeof data === 'string' && data.charAt(0) === '{') {\n    return evaluateExpression(data);\n  } \n  \n  return data;\n}\n\nreturn [\n  {\n    json: resolveData($node[\"MongoDB\"].json),\n  }\n]\n\n"
      },
      "name": "Convert1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        840,
        530
      ]
    }
  ],
  "connections": {}
}

Thank you!

Now i will try to not disturb for a while:)