Mongodb update document

Hey, how are you? Could you help me with something from n8n + mongodb?

Im trying to update a document in mongodb, but I can’t update the field, how and what values ​​to fill in??

I have ID:1, I need to look for this ID in mongodb and update it with the new date.

I had results in the first part of locating the document, however, I am unable to update it.


{
“meta”: {
“instanceId”: “74deeefbe1e9a28c135c1a51a7f46d5666c73d780f44c62bcc30226644397c”
},
“nodes”: [
{
“parameters”: {
“httpMethod”: “POST”,
“path”: “task”,
“options”: {}
},
“id”: “1f223bdc-1392-45a3-a59a-81ccbafc996”,
“name”: “Webhook”,
“type”: “n8n-nodes-base.webhook”,
“typeVersion”: 1,
“position”: [
900,
360
],
“webhookId”: “85d95259-954a-4da-878e-31c42b332ea”
},
{
“parameters”: {
“collection”: “Tarefas”,
“options”: {},
“query”: "={"ID": {{ $json.id_tarefa }}} "
},
“id”: “d87ce920-dfb0-4b5c-b8b0-31bcb1dab0a4”,
“name”: “MongoDB”,
“type”: “n8n-nodes-base.mongoDb”,
“typeVersion”: 1,
“position”: [
1320,
360
],
“credentials”: {
“mongoDb”: {
“id”: “3WsPdmDlKWfSD4”,
“name”: “MongoDB”
}
}
},
{
“parameters”: {
“fields”: {
“values”: [
{
“name”: “id_tarefa”,
“stringValue”: “={{ $json.body.data.task.id }}”
},
{
“name”: “happened_at”,
“stringValue”: “={{ $json.body.happened_at }}”
}
]
},
“include”: “none”,
“options”: {}
},
“id”: “947db979-1d9c-06-a08a-7744982abef6”,
“name”: “Edit Fields”,
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3,
“position”: [
1120,
360
]
},
{
“parameters”: {
“operation”: “update”,
“collection”: “Tarefas”,
“updateKey”: “ID”,
“fields”: “={{ $(‘Edit Fields’).item.json.happened_at }}”,
“options”: {}
},
“id”: “1c64fea6-2c04-41d4-8235-4c6c1a8dec22”,
“name”: “MongoDB1”,
“type”: “n8n-nodes-base.mongoDb”,
“typeVersion”: 1,
“position”: [
1540,
360
],
“credentials”: {
“mongoDb”: {
“id”: “3WsPdmDlv5fSD4”,
“name”: “MongoDB account”
}
}
}
],
“connections”: {
“Webhook”: {
“main”: [
[
{
“node”: “Edit Fields”,
“type”: “main”,
“index”: 0
}
]
]
},
“MongoDB”: {
“main”: [
[
{
“node”: “MongoDB1”,
“type”: “main”,
“index”: 0
}
]
]
},
“Edit Fields”: {
“main”: [
[
{
“node”: “MongoDB”,
“type”: “main”,
“index”: 0
}
]
]
}
}
}

Hi @Allan_Barsanulfo :wave: Can you give an example of the data you’re sending over to the webhook in JSON format, as well? :eyes:

This is the json received in the webhook:

{
    "event": "task:deliver",
    "happened_at": "2023-01-21 19:49:02 -0300",
    "performer": { "id": "john-snow", "email": "[email protected]" },
    "data": {
      "task": {
        "id": 100,
        "board": { "id": 19 },
        "board_stage": { "id": 18 },
        "project": { "id": 16 },
        "assignees": [
          { "id": "phelip-old", "email": "[email protected]" },
        ],
        "user": { "id": "jack-doe"},
      }
    }
  }

This is the return of the query from the find operation using “id” (“id”: 100):

[
  {
    "_id": "651ed2006b0ee19fcf8",
    "ID": 100,
    "Prioridade": "null",
    "Título": "Test",
    "Urgente": "false",
    "Compartilhada": "false",
    "Criada em": "2014-11-11T23:01:48-02:00",
    "Entrega": "2014-12-03T12:42:18-02:00",
    "Tags": ""
  }
]

Therefore, I would like to update only the field value “Entrega” from the webhook: happened_at ( “happened_at”: “2023-01-21 19:49:02 -0300”,)

1 Like

Hi @Allan_Barsanulfo :wave:

I think the issue here is how you’re setting the Fields property in the MongoDB Update node. Instead of using an expression you have to set the field name, in this case, happened_at. That should match the incoming value called happened_at, and write it to MongoDB. :+1:

However, I still don’t understand how this configuration is done, as I just need to update the new value in the “Entrega” field

Hi @Allan_Barsanulfo - if your field is named delivery instead of happened_at in MongoDB, you have to rename the incoming field to delivery and use that in MongoDB’s Fields parameter :+1:

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