Thursday, February 15, 2024

n8n MongoDB Node - "insert"

This might be the first of several "missing manual" posts for n8n.  The product itself is amazing, but the documentation, and sometimes even the related forum posts are miserably lacking.  This one is about the MongoDB "insert" function.  It should be straightforward and easy, but it does NOT work like you would expect.  If you mouse-hover over the question-mark icons on each input field in the editor form, it helps a little, but still might get you past "trial and error" mode.

What the Docs COULD Say

The value of the "Fields" input must be a comma-separated list of attribute names from the root level of one item in the input data for the node.  For example, if the input data contained this:

[
  {
    "name": "Bob"
    "address": {
      "street": "123 Main St",
      "city": "Lincoln",
      "state": "NE",
      "zip": "68516"
    },
    "age": 42
  },
  {
    "name": "Susan"
    "address": {
      "street": "456 Smith Ave",
      "city": "Springfield",
      "state": "IL",
      "zip": "62704"
    },
    "age": 38
  }
]

...then the MongoDB node insert "Fields" could be...

name, age

 ...and 2 JSON objects (without "address": "value" attributes) would be inserted into the specified collection in MongoDB.

Option: "Use dot notation" (Select/add, then enable)

  • Allows nested attributes to be selected as an included field (excluding peer attributes in the sub-object).
  • This does NOT flatten the structure, but filters which nested fields get included
  • For example, field list could be:

name, age, address.zip

Option: "Date Fields"

  • This is a second comma-separated list field names, but these get converted to MongoDB BSON date values in the inserted object... if you need that for some reason.

 Other notes:

  • Whitespace between fields in the comma separated list of field names will be ignored.
  • If dot notation is specified as a field name without the "Use dot notation" option enabled, the output will most likely include a null attribute like: "somenestedthing.someattribute": null

The Actual "Docs" (Barely)

https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.mongodb/

As of 2/2024, this doesn't tell you much of anything.  If it were the owner's manual for a car, it wouldn't give you much past:  Up to 4 people can fit in it.  It goes.  Hopefully it also stops.

Forums

  • Subject: "Insert JSON data to MongoDB inserting empty values"
    • https://community.n8n.io/t/insert-json-data-to-mongodb-inserting-empty-values/24594
    • This post asks why a single JSON document can't just be inserted into MongoDB as is.
    • The answer runs off in the weeds with some nonsense about splitting things into fields.
  • Subject:  "MongoDB Insert key/value formatting"
    • https://community.n8n.io/t/mongodb-insert-key-value-formatting/4948
    • This post is again from someone confused that the input for a MongoDB insert isn't some sort of reference to both Keys and Values (like a whole JSON document... e.g. $json)
    • The answer is a link to some set of examples that seems completely unrelated.
  • Subject: "MongoDB insert data?"
    • https://community.n8n.io/t/mongodb-insert-data/192
    • This might have been asked before the MongoDB built in node/integration was added.
    • The answer mentions the "new" integration node, but still doesn't explain how to use it.
  • Subject: "How to use the “function node” with a “set node” and a “mongodb insert” in combination?"
    • https://community.n8n.io/t/how-to-use-the-function-node-with-a-set-node-and-a-mongodb-insert-in-combination/21059
    • This SHOULD be included in a search for "fields.split is not a function", but that important bit of text is buried in a screen shot.
    • The answer to the problem most people seem to be having is here if you look carefully, but its one of those things you probably see only after you've figured it out anyway.
  • Subject: "How to insert properly data in mongoDB?"
    • https://community.n8n.io/t/how-to-insert-properly-data-in-mongodb/4052
    • Yet another cry for help, because there is no explanation of how this node works.
    • The replies don't help, and the topic is locked (which is why I'm writing here instead of answering there, btw.)