7

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
      }
    }
3
  • what is NumberLong value? can you give an example? Commented Jan 21, 2017 at 9:18
  • From mongoDb Shell if query the data Long value will represented like NumberLong in mongo. For Ex : The above value should get inserted like below in Mongo DB { "_id" : ObjectId("58832821e8f07b1a235ae273"), "myId" : "AB223", "Timestamp" : NumberLong(1484990497160), "userData" : {.....} } Commented Jan 21, 2017 at 9:24
  • stackoverflow.com/a/21870772/2965883 Commented Jan 21, 2017 at 14:42

1 Answer 1

10

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.