# Inserting data with timestamp in MongoDB

**URL:** <https://discourse.nodered.org/t/inserting-data-with-timestamp-in-mongodb/62618>\
**Category:** General\
**Created:** [14 May 2022 09:28 UTC](https://discourse.nodered.org/t/inserting-data-with-timestamp-in-mongodb/62618 "2022-05-14T09:28:17Z")\
**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:** [14 May 2022 09:28 UTC](https://discourse.nodered.org/t/inserting-data-with-timestamp-in-mongodb/62618/1 "2022-05-14T09:28:17Z")

</div>

Hi all,  
I am trying to learn the basics of MongoDB to insert some process data from a PLC so that another application of a partner uses this data in realtime.  
I would use Node-RED to read the data from the Siemens PLC (using S7 node) and inserting it into the MongoDB data base. I am trying the nodes of Node-red-node-mongodb.

I have created a data base for trials with a timeseries collection. So the inserted document must include a time field, but I am not being able to do it correctly.  
I inject the "timestamp" value, but it isn't recogniced as a correct time field and cannot be inserted in the MongoDB data base.

How should I do it? (please, take into account that my programming background is very poor).

Thanks in advance and best regards

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [15 May 2022 12:33 UTC](https://discourse.nodered.org/t/inserting-data-with-timestamp-in-mongodb/62618/2 "2022-05-15T12:33:11Z")

</div>

> [@e-Duardo](#):
>
> I inject the "timestamp" value, but it isn't recogniced as a correct time field and cannot be inserted in the MongoDB data base.

hi .. are you sending your "timestamp" field as a Date or is it a String and its failing ?  
I did some quick test with a timeseries collection and this worked for me

Function node to prepare a msg.payload for the mongodb out node set to **Insert** :

```auto
msg.payload = {
    "timestamp": new Date(),
    "value1": 33,
    "value2": 12
}

return msg;

```

**Flow:**

```auto
[{"id":"dc742398782aab73","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1080,"wires":[["07beb63e4e603c82"]]},{"id":"07beb63e4e603c82","type":"function","z":"54efb553244c241f","name":"function 1","func":"msg.payload = {\n \"timestamp\": new Date(),\n \"value1\": 33,\n \"value2\": 12\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":1080,"wires":[["79b70be061685cef","676f99158b209fc6"]]},{"id":"79b70be061685cef","type":"debug","z":"54efb553244c241f","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":980,"wires":[]},{"id":"676f99158b209fc6","type":"mongodb out","z":"54efb553244c241f","mongodb":"39adf9d0.a8c876","name":"","collection":"test2","payonly":true,"upsert":false,"multi":false,"operation":"insert","x":730,"y":1080,"wires":[]},{"id":"39adf9d0.a8c876","type":"mongodb","hostname":"localhost","topology":"direct","connectOptions":"useUnifiedTopology=true","port":"27017","db":"nodered","name":""}]

```

If you give us more information of the stucture of data that come from the S7 node maybe we can help  
to tweak the code based on your project.

---

<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:** [14 July 2022 12:33 UTC](https://discourse.nodered.org/t/inserting-data-with-timestamp-in-mongodb/62618/3 "2022-07-14T12:33:13Z")

</div>

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