Correct way to send operators ($) in the MongoDB? - Find and Update -

Describe the problem/error/question

I would like to know the correct way to send operators, in my example case, $addToSet when using the MongoDB update node. None of my attempts were successful.

What is the error message (if any)?

No errors, it’s just being inserted according to the default format (str, Object, etc).

Please share your workflow

Share the output returned by the last node

https://i.imgur.com/6fWqJmN.png

Information on your n8n setup

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

I don’t think there is a way to do it with the MongoDB node in n8n. The type of call it makes requires a document with concrete values, not MongoDB functions/operators.

As a workaround, you can:

  1. Read the document in
  2. Change the array/set value in a similar way to what $addToSet does
  3. Update the document

If you have a document in a collection named TestAddToSetWorkaround with the following content:

{
    _id: ObjectId('68220786603c3f574d8d0d75'),
    documentKey: '1234',
    otherField1: 'leave this alone',
    arrayFieldToAlter: [
        'keep this'
    ],
    otherField2: 'leave this alone too'
}

This workflow would give you a working “starting point” with the steps to add a new, unique value to the arrayFieldToAlter array element of the document.

2 Likes

Thank you! Even though it wasn’t the solution I was hoping for, it was still very helpful to learn about this limitation. I’ll submit a feature request for it. I think this would be relevant information to include in the documentation as well.

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