Hi guys,
I'm trying to insert a (long) series of timestamps and values into my local mongo db,
but rather than saving the timestamps as integers they are saved as doubles.
The function I am using contains the following:
var now = new Date().getTime();
msg.payload = {
"symbol": "MySmbol",
"data": {
"timestamps": [ now, now+100, now+200],
"values": [Math.random(), Math.random(), Math.random()]
}
}
return msg;
According to the mongoDB documentation I can make use of the NumberLong function, see Data Types in the mongo Shell — MongoDB Manual
However, when I use NumberLong in the assigment for timestamp:
"timestamp": [ NumberLong("'" + now + "'")], // "NumberLong('" + (now+100) + "')",
... I receive this error in the
"ReferenceError: NumberLong is not defined (line 6, col 22)"
And when I use this line:
"timestamp": [ "NumberLong('" + now + "')", "NumberLong('" + (now+100) + "')", "NumberLong('" + (now+200) + "')"],
the inserted data in each timestamp field looks as follows
timestamp:
[0] - NumberLong('1611424544678')
[1] - NumberLong('1611424544778')
[2] - NumberLong('1611424544878')
...which is what I neither want.
Anyone having an idea how to get integers into the mongo db?
My flow looks as follows:
You can copy the flow here:
[{"id":"1aa86ea6.d665a1","type":"debug","z":"9ba23ed4.19e7f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":550,"y":540,"wires":[]},{"id":"4207f55c.7ac6e4","type":"function","z":"9ba23ed4.19e7f","name":"insert","func":"var now = new Date().getTime();\n\nmsg.payload = {\n \"symbol\": \"Usa500\",\n \"data\": {\n \"timestamp\": [ NumberLong(\"'\" + now + \"'\"), NumberLong(\"'\" + (now+100) + \"'\"), NumberLong(\"'\" + (now+200) + \"'\")],\n //\"timestamp\": [ now, now+100, now+200],\n \"values\": [Math.random(), Math.random(), Math.random()]\n //\"high\": [Math.random(), Math.random(), Math.random()],\n //\"low\": [Math.random(), Math.random(), Math.random()],\n //\"close\": [Math.random(), Math.random(), Math.random()],\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":600,"wires":[["1aa86ea6.d665a1","1d542582.bcb82a"]]},{"id":"28f711dc.7848be","type":"inject","z":"9ba23ed4.19e7f","name":"Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":150,"y":600,"wires":[["4207f55c.7ac6e4"]]},{"id":"1d542582.bcb82a","type":"mongodb out","z":"9ba23ed4.19e7f","mongodb":"bab2560a.6eb3c8","name":"Insert into Test1","collection":"Test1","payonly":true,"upsert":false,"multi":false,"operation":"insert","x":580,"y":600,"wires":[]},{"id":"bab2560a.6eb3c8","type":"mongodb","hostname":"kaspar","topology":"direct","connectOptions":"authSource=admin","port":"27017","db":"SYMBOL_HISTORY","name":"MongoDB"}]
Cheers
Marcel