MongoDB in Workflows

I’m attempting to manipulate a MongoDB Database/Collection from within a workflow and having problems. I have precious little inexperience with databases.

I have automatic device creation working with a /register endpoint; works fine. From within my /register workflow I’m attempting to create a MongoDB document corresponding to the device using fields from the payload. I can create a document using a JSON payload with a unique _id with InsertOne “{ “_id”:”{{data.body.serialNumber}}", “Hello”:“World” }". The Hello:World pair will also be created in the document if it’s hard-coded as shown.

However, I would like to create a number of additional document fields from the payload but I can’t get this working. For instance I would like to iterate through “data.device.attributes” and create document fields for each of my attributes. How do I go about this? UpdateOne and UpdateMany complain of “E11000 duplicate key error” as though I’m attempting to create a new document when I use “{ “_id”: “{{data.body.serialNumber}}” }” in the Query Argument field of the MongoDB node. My understanding is that the first argument in the MongoDB workflow node is the document to update and the second argument are the field:value pair(s) to add to the document. Where’s my error?

With UpdateOne:
Query Argument: { “_id”: “{{data.body.serialNumber}}” }
Update to Apply: { “test” : “test” }

Should this not add a field:value pair “test”:“test” in the Query Argument’s document?


Hi @Jason_Farque,

Per the documentation for updateOne, you are correct that the first argument (filter) is the document to update, but you are missing a query selector within filter. In this case, to return the document that matches the _id value, you would form your query argument as:

{ "_id": { "$oid": "{{data.body.serialNumber}}" } }

To iterate over your data, I would recommend using the Loop Node and possibly an Array Node or Function Node to form the data into an object or an array on the payload, then you can reference the parent key to include this data in the MongoDB Update Argument.

Let me know if you have any further questions or difficulty with this, I’m happy to explain further!