Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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.
Advertisements
