# MongoDB update (mongodb3)

**URL:** <https://discourse.nodered.org/t/mongodb-update-mongodb3/64938>\
**Category:** General\
**Created:** [11 July 2022 10:17 UTC](https://discourse.nodered.org/t/mongodb-update-mongodb3/64938 "2022-07-11T10:17:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![e-Duardo](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@e-Duardo](https://discourse.nodered.org/u/e-Duardo)\
**Post date:** [11 July 2022 10:17 UTC](https://discourse.nodered.org/t/mongodb-update-mongodb3/64938/1 "2022-07-11T10:17:15Z")

</div>

Hi all,  
I am learning to use MongoDB to store data, and now I am having an issue with the "update" method. I am using the nodes from "node-red-contrib-mongodb3".

I have inserted some documents, which have some static data (descriptive information of what is stored in the document) and dynamic values, which represent the actual status of the variables.

So, I am using the funcion "update", and I pass the msg.payload containing an array, in which the first position selects the identifier of the document (I don't use "\_id", but another static value), and the second position of the array contain the dynamic values that must be updated.  
The document is something like that.

```auto
{
   "_id":123456
   "timestamp": 0,
  "tag": "ABCD",
  "type": "read",
  "units": "ºC",
  "freq": 12,
  "value": 0,
  }

```

Here, "value" and "timestamp" are the dynamic values that are updated. And I use "tag" as identifier.

So, the payload that I am passing to the "mongdb3 in" node, which the operator "update" is:

```auto
msg.payload= [{"tag": "ABCD"} ,{"value":msg.payload, "timestamp": new Date()}];

```

The result is that, in the document containing "tag": "ABCD", not only the fields "value" and "timestamp" are updated, but the other fields of the document are removed. So that the document becomes like that:

```auto
{ "_id":123456, 
  "value": 55.9,  
  "timestamp": { "$date": { "$numberLong": "1657534300702" } }
}

```

How can I avoid it to happen?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![e-Duardo](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@e-Duardo](https://discourse.nodered.org/u/e-Duardo)\
**Post date:** [11 July 2022 11:18 UTC](https://discourse.nodered.org/t/mongodb-update-mongodb3/64938/2 "2022-07-11T11:18:23Z")

</div>

> [@e-Duardo](#):
>
> `msg.payload= [{"tag": "ABCD"} ,{"valu`

Hi,  
I reply myself because I found the error. I just forget using the operator "$set" 🤦‍♂️

```auto
msg.payload= [{"tag": "ABCD"} ,{"$set":{"value":msg.payload, "timestamp": new Date()}}];

```

Now it works as expected.

Best regards!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 July 2022 11:18 UTC](https://discourse.nodered.org/t/mongodb-update-mongodb3/64938/3 "2022-07-25T11:18:44Z")

</div>

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