How to print NumberLong value in MongoDB?

In MongoDB, the NumberLong() wrapper handles 64-bit integers that exceed JavaScript's safe integer range. To print NumberLong values, use the toString() method or direct printing to display the value properly.

Syntax

var variableName = NumberLong("longNumber");
variableName.toString();

// Or direct printing
print(variableName);

Example 1: Using toString() Method

Create a NumberLong variable and display it using toString() ?

var number = NumberLong("231231231231121231");
number.toString();
NumberLong("231231231231121231")

Example 2: Another NumberLong Value

Display a different NumberLong value ?

var anotherNumber = NumberLong("765765765765567576");
anotherNumber.toString();
NumberLong("765765765765567576")

Example 3: Direct Printing

Use the print() function to display NumberLong values directly ?

var longValue = NumberLong("123456789012345678");
print(longValue);
NumberLong("123456789012345678")

Key Points

  • toString() converts NumberLong to its string representation for display
  • print() function can directly output NumberLong values
  • NumberLong preserves precision for large integers beyond JavaScript's safe range

Conclusion

Use toString() or print() methods to properly display NumberLong values in MongoDB shell. Both approaches maintain the NumberLong wrapper format while showing the actual 64-bit integer value.

Updated on: 2026-03-15T03:31:45+05:30

752 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements