Inserting data with timestamp in MongoDB

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

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 :

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

return msg;

Flow:

[{"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.

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