Putting key:value to MongoDB

Describe the problem/error/question

I am trying to get data from webhook request body and put them to Mongo.
So my body data looks like {field1: value1, field2: value2}
When I try to insert data to Mongo i have troubles.
When I construct output fields like:
{{ {"field1" : $json.body.field1, "field2" : $json.body.field2, } }}
I get
[object Object] : null
is sent to Mongo
What is correct way to put filed:value (or in general – custom JSON object) to MongoDB?

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @kimsanov :wave: Welcome to the community :tada:

Could you share your n8n version with us, and how you’re running n8n? Is this through n8n cloud, or are you self-hosting? If you’re self-hosting, can you tell us how you’re running n8n (Docker, npm, etc)?

Sharing your workflow would be helpful, too!

Hello! I am just testing n8n. So I run it via:

npx n8n

My test workflow is simple. Get JSON from https://jsonplaceholder.typicode.com/users/1 , extract arbitrary fields (e. g. username and email) and try to put it to mongo.
As I suppose - putting data to DB works well for SQL DBs (because you supply data as row of elements in order predefined by db table schema). In case of mongo - you need to send to db kind of JSON object. Something like key-value pair(s). So I cannot figure out how to do this.

What I expect - get in mongo record like
{"username": "abc", "email": "[email protected]"}

What I get
{"abc": null, "[email protected]": null}

Hi @kimsanov :wave: Thanks for that information!

Instead of {{ $json.username }}, {{ $json.email }}, can you enter just the actual names of the fields to insert (so username, email) and see if specifying the field names fixes this up for you?

1 Like

AAAAAA!!! Solution was so simple! Thanks! I dragged fields, that’s why I got this {{}} thing. Thank you!


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