How to use the "function node" with a "set node" and a "mongodb insert" in combination?

Describe the issue/error/question

How to insert the “output” from the “function node” into the mongodb insert to push the data into the database? WIth a “set node” or “without”.

Steps i want to go:

  1. Make API Call (working)
    1.1 Response is a JSON Object (working)
    1.2 Use function to extract key, value paris (working)
    1.3 use set node to create a big array (not working)
    1.4 use mongodb insert call to push array into mongodb (not working)

i dont understand the “set” module while working with returned data from a function.
It is adding up the fieldname into the insert call.

Image for the Workflow, as i can’t inser it accordlingy(noob? :D))

image

This is the “code / function node”:

This is the “set node”:


Why is the paired items now in it? I’ve read the docs but don’t need it at all.

This is the mongodb call:

I can’t access the “fieldsToInsert” Variable from the node before and also the informations in the node before (output in set node) aren’t what i want. I just want to insert the object from the "set node input.

Now i believe ive found a bug:

I cant create a key value pair in this screenshot. If i try to insert just the values i get this error, if i try to insert it by key values as “expression” like: {{ gpcode: $json[“data”][“id”] }}
isn’t working as well. I get only “gpcode” returned instead of “gpcode:4036” as id.

My question is again:
How to insert the “output” from the “function node” into the mongodb insert to push the data into the database?

Hope this is clear and specific enough.

If i try to insert the workflow, the code doesn’t resolve. I am doing something wrong.

The next thing is:
The mongodb Node has only the insert function. mongodb has something called “insertmany” to make it possible to insert several documents with one call.
Maybe you can consider this as well for future updates.

Information on your n8n setup

  • n8n version: using the cloud
  • Database you’re using (default: SQLite): mongodb
  • Running n8n with the execution process [own(default), main]: triggering the workflow manually

Hi @DrTrabant, welcome to the community :tada:

Let’s unpack this.

Why is the paired items now in it? I’ve read the docs but don’t need it at all.

You can simply ignore these if you don’t need them.

I cant create a key value pair in this screenshot. If i try to insert just the values i get this error, if i try to insert it by key values as “expression” like: {{ gpcode: $json[“data”][“id”] }}

When using an expression in the “Fields” field of the MongoDB node, you’d need to make sure to provide a string. n8n will try to split this string on each comma, but this would fail for a numeric value like in your screenshot. The values provided here would specify the keys. n8n would then read the corresponding values from the incoming items and insert the resulting pairs as a new document in MongoDB.

So in other words, you’d need to bring your data into a “table-like” structure first before inserting them into MongoDB.

Here’s a quick example workflow inserting dummy data into MongoDB:

When looking at the example data you can see how I am passing on a JSON structure consisting of key/value pairs here, then in the “Fields” field I specify the names of the fields I want to insert into MongoDB:

This results in MongoDB documents like this:

image

So you’d need to bring your existing data into a similar structure, where each field you’re trying to add to your MongoDB collection exists on the incoming n8n items. If you need any assistance with this it’d be great if you could share the JSON data returned by your HTTP Request node so I can have a go at writing a short transformation snippet here (or recommend other ways to achieve this).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.