How to insert an 8-byte integer into MongoDB through JavaScript shell?

To insert an 8-byte integer into MongoDB through JavaScript shell, use the NumberLong() constructor to create a 64-bit integer value. This ensures proper storage of large integer values that exceed JavaScript's standard number precision.

Syntax

anyVariableName = {"yourFieldName": new NumberLong("yourValue")};

Example

Create a variable with an 8-byte integer ?

userDetail = {"userId": new NumberLong("98686869")};
{ "userId" : NumberLong(98686869) }

Display Variable Value

Display the variable using the variable name ?

userDetail
{ "userId" : NumberLong(98686869) }

Display the variable using printjson() ?

printjson(userDetail);
{ "userId" : NumberLong(98686869) }

Key Points

  • NumberLong() creates 64-bit integers for values exceeding JavaScript's safe integer range.
  • Pass the value as a string to NumberLong() to avoid precision loss.
  • Both variable name and printjson() display the same formatted output.

Conclusion

Use NumberLong() constructor to create 8-byte integers in MongoDB JavaScript shell. This ensures accurate storage and retrieval of large integer values without precision issues.

Updated on: 2026-03-15T01:08:09+05:30

273 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements