Getting Nulls in mongo DB with json

Describe the problem/error/question

I keep getting Null output from my json input into mongodb.

What i’m trying to achieve is to just grab a datafeed from a distributor that sells hardware and put it into mongodb for an AI agent to reference.

{
“MMT Code”: “{{ $json[‘MMT Code’] ?? ‘’ }}”,
“SKU”: “{{ $json.SKU ?? ‘’ }}”,
“Manufacturer”: “{{ $json.Manufacturer ?? ‘’ }}”,
“Category”: “{{ $json.Category ?? ‘’ }}”,
“Description”: “{{ $json.Description ?? ‘’ }}”,
“Cost”: {{ $json.Cost ?? 0 }},
“Stock”: {{ $json.Stock ?? 0 }}
}

What is the error message (if any)?

Please share your workflow




Share the output returned by the last node

Information on your n8n setup

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

Please include the workflow. If getting the data requires credentials, please pin received data to the node and then share the workflow.

I assume on the first image you are showing some manually set data?
What is on the second image?

From what I can see you are dealing with json string instead of a json object.

Sorry like this?

{
“nodes”: [
{
“parameters”: {
“rule”: {
“interval”: [
{
“triggerAtHour”: 6
}
]
}
},
“type”: “n8n-nodes-base.scheduleTrigger”,
“typeVersion”: 1.2,
“position”: [
-1360,
-240
],
“id”: “c6b9c79a-fadc-47fb-8c30-12e7f4af2232”,
“name”: “Schedule Trigger”
},
{
“parameters”: {
“operation”: “insert”,
“collection”: “test”,
“fields”: “=MMT Code:{{ $json[‘MMT Code’] }},\nSKU:{{ $json.SKU }},\nManufacturer:{{ $json.Manufacturer }},\nCategory:{{ $json.Category }},\nDescription:{{ $json.Description }},\nCost:${{ $json.Cost }},\nStock:{{ $json.Stock }}\n”,
“options”: {}
},
“type”: “n8n-nodes-base.mongoDb”,
“typeVersion”: 1.2,
“position”: [
-256,
-240
],
“id”: “c4560110-5a70-4bf3-adca-31c62b288dfc”,
“name”: “MongoDB”,
“alwaysOutputData”: true,
“credentials”: {
“mongoDb”: {
“id”: “OBXNgJvwfIUFijOr”,
“name”: “MongoDB account”
}
}
},
{
“parameters”: {
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “61b356e7-751a-49f7-accd-348b32f50146”,
“leftValue”: “”,
“rightValue”: “”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
},
“options”: {}
},
“type”: “n8n-nodes-base.filter”,
“typeVersion”: 2.2,
“position”: [
-800,
-240
],
“id”: “d601ffc7-9536-411b-8e5d-466d18ac16d6”,
“name”: “Filter”
},
{
“parameters”: {},
“type”: “n8n-nodes-base.limit”,
“typeVersion”: 1,
“position”: [
-640,
-240
],
“id”: “a6aa0281-5c55-4cb4-a046-60b0f0283b39”,
“name”: “Limit”
},
{
“parameters”: {
“values”: {
“string”: [
{
“name”: “MMT Code”,
“value”: “={{ $json[‘MMT Code’] }}”
},
{
“name”: “SKU”,
“value”: “={{ $json[‘Man.Code/SKU’] }}”
},
{
“name”: “Manufacturer”,
“value”: “={{ $json.Manufacturer }}”
},
{
“name”: “Category”,
“value”: “={{ $json.Category }}”
},
{
“name”: “Description”,
“value”: “={{ $json.Description }}”
}
],
“number”: [
{
“name”: “Cost”,
“value”: “={{ $json[‘Your Buy Ex. GST’] }}”
},
{
“name”: “Stock”,
“value”: “={{ $json[‘Available (Qty)’] }}”
}
]
},
“options”: {}
},
“name”: “Set”,
“type”: “n8n-nodes-base.set”,
“position”: [
-464,
-240
],
“typeVersion”: 1,
“id”: “39198cef-44b5-4c66-ac78-67b4766d75fa”,
“alwaysOutputData”: true
},
{
“parameters”: {
“url”: “https://www.mmt.com.au/datafeed/index.php?lt=s&ft=csv&tk=94P0C1R223OF7AI59BT94903AC004E4B4A%20D09%2083A%2046B%20D80%20648%2031F%2075D%20665F9461C558F25AE”,
“authentication”: “genericCredentialType”,
“genericAuthType”: “httpBasicAuth”,
“options”: {
“response”: {
“response”: {}
}
}
},
“type”: “n8n-nodes-base.httpRequest”,
“typeVersion”: 4.2,
“position”: [
-1168,
-240
],
“id”: “d82217e3-6ba0-43d1-baba-f37b6c689b84”,
“name”: “HTTP Request - MMT Datafeed1”,
“alwaysOutputData”: true,
“credentials”: {
“httpBasicAuth”: {
“id”: “QGZIgLPy4y1QEzRA”,
“name”: “MMT Auth”
}
}
},
{
“parameters”: {
“options”: {}
},
“type”: “n8n-nodes-base.extractFromFile”,
“typeVersion”: 1,
“position”: [
-976,
-240
],
“id”: “24955269-88ce-4d9d-bdf8-ca4de890f9f1”,
“name”: “Convert CSV to Json”,
“alwaysOutputData”: true
}
],
“connections”: {
“Schedule Trigger”: {
“main”: [
[
{
“node”: “HTTP Request - MMT Datafeed1”,
“type”: “main”,
“index”: 0
}
]
]
},
“Filter”: {
“main”: [
[
{
“node”: “Limit”,
“type”: “main”,
“index”: 0
}
]
]
},
“Limit”: {
“main”: [
[
{
“node”: “Set”,
“type”: “main”,
“index”: 0
}
]
]
},
“Set”: {
“main”: [
[
{
“node”: “MongoDB”,
“type”: “main”,
“index”: 0
}
]
]
},
“HTTP Request - MMT Datafeed1”: {
“main”: [
[
{
“node”: “Convert CSV to Json”,
“type”: “main”,
“index”: 0
}
]
]
},
“Convert CSV to Json”: {
“main”: [
[
{
“node”: “Filter”,
“type”: “main”,
“index”: 0
}
]
]
}
},
“pinData”: {},
“meta”: {
“templateCredsSetupCompleted”: true,
“instanceId”: “78d7940082fc41097e317a240f1f7c16db06d8b63cbf9356b656a69770e8a784”
}
}

I managed to piece it together.

From what I can tell (even though you haven’t pinned any data), in the mongo node you are not referencing the fields, but rather compiling a text with field names AND values. Instead you should only mention the fields.

Is this how you reference?

{
“product”: {
“MMT_Code”: “{{ $json[‘MMT Code’] ?? ‘’ }}”,
“Manufacturer”: “{{ $json.Manufacturer ?? ‘’ }}”,
“SKU”: “{{ $json.SKU ?? ‘’ }}”
}
}

A picture is worth a 1k words:

Here:

This

will give you this:

Thank you so much that worked!

I noticed your MongoDB node is trying to insert a string with field names and values, which is likely causing the null output. Instead, in the MongoDB node’s “Fields” parameter, you should only list the field names (e.g., MMT Code, SKU, Manufacturer, Category, Description, Cost, Stock) and ensure your JSON data is correctly mapped in the Set node.

Yes thank you so much Fatima that is what i ended up doing :slight_smile: