Inserting integers into mongo db with node-red-node-mongodb module

The link you sent above is from Mongo Shell .. mongo Shell has access to those internal mongo functions
like NumberLong.

The node-red Mongo node doesnt (expose) them especially in a preceding Function node.

A workaround would be to edit your settings.js file in the .node-red folder and under functionGlobalContext add the following ..

functionGlobalContext: {

        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
      //  moment:require('moment'),
        ObjectId:require('mongodb').ObjectID,
        NumberInt:require('mongodb').Int32,
        NumberLong:require('mongodb').Long

    },

And in the Function node in order to use them :

var now = new Date().getTime() //.toString();

let NumberInt = global.get('NumberInt')
let NumberLong = global.get('NumberLong')

msg.payload = {
    "symbol": "Usa500",
    "data": {
        //"timestamp": [ NumberLong("'" + now + "'"), NumberLong("'" + (now+100) + "'"), NumberLong("'" + (now+200) + "'")],
        "timestamp": [ NumberLong(now), NumberLong(now), now],
        "values": [Math.random(), Math.random(), Math.random()]
        //"high": [Math.random(), Math.random(), Math.random()],
        //"low": [Math.random(), Math.random(), Math.random()],
        //"close": [Math.random(), Math.random(), Math.random()],
    }
}
return msg;

The Mongodb node uses the Node js MongoDb Driver and the documentation to it is here instead

Also read this interesting article

I did try all this before commenting yesterday and surely my intension wasnt to sidetrack from the actual problem :wink:

[EDIT]
i made a mistake above .. i think you use NumberLong.fromNumber(now) to force save it as Long.

As Steve explained, its the same as using Double .. its the same amount of space in the Db?!
Im new to mongodb and its data types .. for sure Int32 isn't big enough to store your timestamp so ..

2 Likes