Mongo add full json

HI

I am trying to add the full Json set via the mongo ingration node.

But my question is how do make so it add the full jason without that i need to all all fields into the node?

monogo3

Sorry, I do not fully understand what you mean. But something like you want to save something to MongoDB but not all the data, just some of it?

If that is it, then you can either just define the data you want with the Set-Node and make sure to set “Keep Only Set” to true. As an alternative, you could also use the Function-Node.

Does that help?

Hi Thanks for you answer

I would like to add the full json without need to specify the fields in the Mongo node.

I dont see the alternative you mention.

Most likly me that dont get it:)

Kind regards
Mattias

Ah now I understand what you mean. In that case you can simply set an expression like this:

{{Object.keys($node["InputNodeName"].json).join(',')}}

So like in this example workflow:

{
  "nodes": [
    {
      "parameters": {
        "operation": "insert",
        "collection": "TestCollection",
        "fields": "={{Object.keys($node[\"Mock-Data\"].json).join(',')}}"
      },
      "name": "MongoDB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        1000,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json = {\n  a: 1,\n  b: 2,\n  c: 3\n};\nreturn items;"
      },
      "name": "Mock-Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        400
      ]
    }
  ],
  "connections": {
    "Mock-Data": {
      "main": [
        [
          {
            "node": "MongoDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

That should do the trick.

Thanks you works great !

Awsome software:)

Just hope that we can solve the issue so we can have static webhooks soon:=)

Thanks a lot, that is great to hear!

Yes, I hope we can look into this soon as it could also help to partly solve some scaling issues.

Hi I got a little new issue

Everything worked fine as long as the there ware not to many level

So what i mean is that it worked when i had only the raw data

like

items[0].json = {
  a: 1,
  b: 2,
  c: 3
};
return items;

But when i get a Json like this and would only like to store in mongo what is below body that mean maindata (and whats under) and other data {and what is under)

items[0].json = {

"body":{
"maindata":{
  "a": "1",
  "b": "2",
  "c": "3"
  },
"otherdata":
  {
  "a": "1",
  "b": "2",
  "c": "3"
  }
},
"headers":{}
};
return items;

I tried
{{Object.keys($node["Webhook"].json["body"]).join(', ')}} but then did i only got added main data and other data as headings but NOT the data under them

I might be abit dumb here but :slight_smile:

Sorry but I do not understand. Only the main level keys have to be set in “Fields”. It then automatically adds all data underneath it. So the expression does not have to change.

To be sure I just tested it myself. And all lower levels got added just fine.

Here my simple test:

{
  "nodes": [
    {
      "parameters": {
        "collection": "workflow",
        "query": "{\"name\": \"test3\"}"
      },
      "name": "MongoDB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        1050,
        100
      ],
      "credentials": {
        "mongoDb": ""
      }
    },
    {
      "parameters": {
        "functionCode": "items[0].json = {\n\"name\": \"test3\",\n\"body\":{\n\"maindata\":{\n  \"a\": \"1\",\n  \"b\": \"2\",\n  \"c\": {\n    \"c2\": {\n      \"c3\": \"3\"\n    }\n  }\n  },\n\"otherdata\":\n  {\n  \"a\": \"1\",\n  \"b\": \"2\",\n  \"c\": \"3\"\n  }\n},\n\"headers\":{}\n};\nreturn items;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        660,
        100
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "workflow",
        "fields": "={{Object.keys($node[\"Function\"].json).join(',')}}"
      },
      "name": "MongoDB1",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        850,
        100
      ],
      "credentials": {
        "mongoDb": ""
      }
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "MongoDB1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB1": {
      "main": [
        [
          {
            "node": "MongoDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I know what he means. He has an JSON object with nested objects. He wants to only populate one of the nested objects into the database.

Base Object:

{
  "event": { .. },
  "location": { .. },
  "area": { .. }
}

Store location in mongo db:

{
   "shipID": "..",
   "locationKey": "..",
   "other": ".."
}

Thanks a lot @jwillmer! That is very helpful

@Mattias_Larsson think now I understand better. To allow exactly that the parameter “fields” exist. If you write in it maindata,otherdata then it will only use the two you want.
Or do you want to save “maindata” in one mongo document and “otherdate” in a separate?

That’s why we have a good CTO.

I would in this case like to store all the data under body. That mean Inthis case that I would like to store both maindata and othedata Inthe same mongo record. In the future this might be different but for now that is the case :).

Kind regards
Mattias

Ah ok understand. Then you have to do it like this:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json = {\n\"name\": \"test3\",\n\"body\":{\n\"maindata\":{\n  \"a\": \"1\",\n  \"b\": \"2\",\n  \"c\": {\n    \"c2\": {\n      \"c3\": \"3\"\n    }\n  }\n  },\n\"otherdata\":\n  {\n  \"a\": \"1\",\n  \"b\": \"2\",\n  \"c\": \"3\"\n  }\n},\n\"headers\":{}\n};\nreturn items;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        400
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "workflow",
        "fields": "={{Object.keys($node[\"Function1\"].json).join(',')}}"
      },
      "name": "MongoDB1",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        850,
        400
      ],
      "credentials": {
        "mongoDb": "local test mongo"
      }
    },
    {
      "parameters": {
        "functionCode": "return items.map(item => { return { json: item.json.body}});\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        400
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "MongoDB1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like