How to insert timestamp in milliseconds as NumberLong in MongoDB using NodeJS

I'm new to nodejs and trying to inject mongodb to insert the current timestamp in milliseconds using nodejs, which it introduces as a double value. Can someone help me how to insert this NumberLong value.

var data = { myId : uniqueId, Timestamp : Date.now(), ---> This one is getting inserted as double. userData : applicationData } } 

I am also trying to insert like this, but its getting the insert as a 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 } } 
+7
mongodb
source share
1 answer

This is because JavaScript numbers always contain a 64-bit floating point. You can use Mongo driver Long ( https://mongodb.imtqy.com/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 } 
+9
source share

All Articles