1

I'm trying to convert the number 1477958757748.9697 to long in the mongo-shell. The NumberLong function throws an error:

> NumberLong(1477958757748.9697);
2017-06-03T08:52:56.306-0400 E QUERY    [thread1] Error: number 
passed to NumberLong must be representable as an int64_t :                  
@(shell):1:1
>

Is there an alternative to NumberLong? NumberInt for examle does work for integers:

> NumberInt(12.6);
NumberInt(12)                     
>

How can you do that in Mongo?

neoexpert
  • 465
  • 1
  • 10
  • 20
  • 1
    `NumberLong(parseInt(1477958757748.9697))` But really, this is just the "shell". Your language of choice is really responsible for such things. In this case, JavaScript. Which of course the shell uses for a REPL – Neil Lunn Jun 03 '17 at 13:17
  • Yeah thanks, i need it in the shell to create some missing timestamps – neoexpert Jun 03 '17 at 13:21
  • Then `parseInt` is the one since the extra precision is not stored. Unless you really want to "round up". In which case `NumberLong(Math.ceil(1477958757748.9697))`. But really you should be using BSON Date, since it's actually stored as the timestamp value internally and is more useful like that `Date(Math.ceil(1477958757748.9697))`. Don't be fooled by string output, because it's actually a number when stored. Remember it's a REPL – Neil Lunn Jun 03 '17 at 13:27

0 Answers0