Int64 / Long

The Long data type in MongoDB is a 64-bit integer, which is useful when you need to store large integral values beyond the range of the standard int (32-bit integer) data type. The range for the Long data type is from -2^63 to 2^63 - 1. This data type is suitable for applications that require high-precision numerical data, such as analytics and scientific calculations.

Syntax

To define a field with the Long data type in MongoDB, you can use the $numberLong keyword. Here’s an example of a document with a field named largeValue defined as a Long data type:

{
  "largeValue": { "$numberLong": "1234567890123456789" }
}

Usage

You can use the Long data type to store and query large integral values in your MongoDB collections. To insert a document with a Long field, you can use the following syntax:

db.collection.insert({
  largeValue: NumberLong('1234567890123456789'),
});

To query documents that have a Long field with a specific value, you can use the following syntax:

db.collection.find({
  largeValue: NumberLong('1234567890123456789'),
});

Considerations

When using the Long data type in MongoDB, keep the following considerations in mind: