I new to nodejs and trying implement mongodb insert of current timestamp as milliseconds using nodejs its getting inserted as double value. Can anyone help me how to insert this as NumberLong value.
var data = {
myId : uniqueId,
Timestamp : Date.now(), ---> This one is getting inserted as double.
userData : applicationData
}
}
I also try to insert like this but its getting insert as String.
var mongo=require('mongodb');
var Long = mongo.Long;
var data = {
myId : uniqueId,
Timestamp : Long.fromString((Date.now() + "")), ---> This one is getting inserted as String.
userData : applicationData
}
}
This is because JavaScript Numbers are Always 64-bit Floating Point. You can use Mongo driver's Long ( https://mongodb.github.io/node-mongodb-native/api-bson-generated/long.html ) to get rid of this problem.
var Long = require('mongodb').Long;
var current_millies = new Date().getTime();
var data = {
myId : uniqueId,
timestamp : Long.fromNumber(current_millies),
userData : applicationData
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With