Convert NumberLong to String in MongoDB?

In MongoDB, you can convert NumberLong values to strings using two approaches: the toString() method for explicit string conversion, or valueOf() method for numeric value extraction.

Syntax

// Method 1: Convert to String
NumberLong('value').valueOf().toString()

// Method 2: Convert to Number  
NumberLong('value').valueOf()

Method 1: Using toString() for String Conversion

The toString() method explicitly converts the NumberLong to a string representation ?

NumberLong('1000').valueOf().toString();
1000

Method 2: Using valueOf() for Number Conversion

The valueOf() method returns the numeric value of NumberLong ?

NumberLong('1000').valueOf();
1000

Key Differences

Method Return Type Use Case
valueOf().toString() String When you need string representation
valueOf() Number When you need numeric operations

Conclusion

Use valueOf().toString() for explicit string conversion and valueOf() for numeric value extraction. Both methods effectively convert NumberLong but return different data types based on your requirements.

Updated on: 2026-03-15T00:52:23+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements