Display data on chart with mongoDB

#1

How to display a chart of temperature with the set of values between two dates on mongodb database (dateBegin and dateFin)

[{"id":"2cf361c3.2cde4e","type":"function","z":"17169c91.ecab0b","name":"","func":"var begin = msg.payload\nvar end = msg.payload + 86400000\n//global.set(\"begin\", begin);\n//global.set(\"end\", end)\nvar firstmsg = {};\nfirstmsg.payload = \n{\n datestamp:\n {\n $lt: end ,$gte: begin\n \n }\n \n};\nfirstmsg.projection = { \"temperature\": 1, \"_id\": 0};\nfirstmsg.limit = 10000;\nreturn firstmsg;\n","outputs":1,"noerr":0,"x":250,"y":240,"wires":[["1883067d.760e5a"]]},{"id":"24e453c7.53209c","type":"function","z":"17169c91.ecab0b","name":"","func":"var m={};\nvar templist = global.get(\"templist\", templist)\nvar datelist = global.get(\"datelist\", datelist)\n//var length = datelist.length\n\nm.labels = datelist;\nm.series = ['temperature'];\nm.data = [templist];\nreturn {payload:[m]};\n\n","outputs":1,"noerr":0,"x":810,"y":240,"wires":[["38068be2.073194"]]},{"id":"38068be2.073194","type":"ui_chart","z":"17169c91.ecab0b","name":"","group":"dc0fedfc.b613","order":2,"width":"20","height":"10","label":"Temperature chart","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"No Data","dot":false,"ymin":"","ymax":"","removeOlder":"1","removeOlderPoints":"10000","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":770,"y":320,"wires":[["d39afb08.04f7d8"]]},{"id":"d58dec21.24c8b","type":"function","z":"17169c91.ecab0b","name":"","func":"var list = msg.payload;\nvar datelist = []\nvar length = list.length;\nwhile (length > 0) {\n x = list.pop()\n var date=new Date(x.datestamp)\n hours = date.getHours()\n min = date.getMinutes()\n var y = hours + \":\" + min \n datelist.push(y);\n length--;\n}\n// rotat list again\nlist = datelist;\nlength = list.length;\ndatelist = []\nwhile (length > 0) {\n x = list.pop()\n datelist.push(x);\n length--;\n}\n\nglobal.set(\"datelist\", datelist);\nmsg.payload = datelist;\nreturn msg;","outputs":1,"noerr":0,"x":670,"y":240,"wires":[["24e453c7.53209c"]]},{"id":"f349b1b6.7185e","type":"ui_date_picker","z":"17169c91.ecab0b","name":"","label":"DateBegin","group":"dc0fedfc.b613","order":1,"width":0,"height":0,"passthru":true,"topic":"","x":81,"y":240,"wires":[["2cf361c3.2cde4e"]]},{"id":"1883067d.760e5a","type":"function","z":"17169c91.ecab0b","name":"","func":"msg.projection = { \"date\": 1, \"_id\": 0};\nmsg.limit = 10000;\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":240,"wires":[["6c82a6ca.66b728"]]},{"id":"6c82a6ca.66b728","type":"mongodb-node","z":"17169c91.ecab0b","mongodb":"faee05a1.cb9438","name":"display_nom","collection":"temperature","operation":"find","upsert":false,"multi":false,"x":530,"y":240,"wires":[["d58dec21.24c8b"]]},{"id":"d39afb08.04f7d8","type":"debug","z":"17169c91.ecab0b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":380,"wires":[]},{"id":"9b28630b.63d6d","type":"ui_date_picker","z":"17169c91.ecab0b","name":"","label":"DateFin","group":"dc0fedfc.b613","order":1,"width":0,"height":0,"passthru":true,"topic":"","x":71,"y":280,"wires":[["2cf361c3.2cde4e"]]},{"id":"dc0fedfc.b613","type":"ui_group","z":"","name":" Power Day Graph","tab":"11d0020a.e12c9e","order":6,"disp":true,"width":"20","collapse":true},{"id":"faee05a1.cb9438","type":"mongodb-config","z":"","hostname":"127.0.0.1","port":"27017","db":"db","name":""},{"id":"11d0020a.e12c9e","type":"ui_tab","z":"","name":" Power History","icon":"dashboard"}]

my database:

#2

So which bit can't you do?

  1. extract data from database?
  2. reformat data into format for chart?
#3

both: extract and display data from database

#4

This might be a good start https://blog.exploratory.io/an-introduction-to-mongodb-query-for-beginners-bd463319aa4c

1 Like