How to use the Long function from bson

Find comprehensive JavaScript bson.Long code examples handpicked from public code repositorys.

bson.Long is a JavaScript object that represents an integer value that can be larger than the range of standard 32-bit or 64-bit integers.

172
173
174
175
176
177
178
179
180
181
  ctx.type = this.Types.Long;
  const symbolType = this.Symbols.Long.attr.fromBits;
  const rhs = this.checkArguments(
    symbolType.args, this.getArguments(ctx), 'Long.fromBits'
  );
  return bson.Long.fromBits(...rhs);
}

emitRegex(ctx, pattern, flags) {
  return new bson.BSONRegExp(pattern, flags);
fork icon128
star icon793
watch icon35

45
46
47
48
49
50
51
52
53
54
},
NumberLong: function(v) {
  if (v === undefined) {
    v = 0;
  }
  return bson.Long.fromNumber(v);
},
ISODate: function(s) {
  return new Date(s);
},
fork icon121
star icon761
watch icon0

+ 6 other calls in file

How does bson.Long work?

bson.Long is a constructor function that creates a new Long object used to represent a 64-bit two's-complement integer value in JavaScript. Internally, it stores the high and low 32-bit portions of the long as two separate signed 32-bit integers. The Long object also provides methods for performing arithmetic operations on the 64-bit integer value it represents, such as addition, subtraction, multiplication, division, and bitwise operations.

6
7
8
9
10
11
12
13
14
15
const MaxKey = bson.MaxKey;
const Binary = bson.Binary;
const BSONRegExp = bson.BSONRegExp;
const Code = bson.Code;
const Timestamp = bson.Timestamp;
const Long = bson.Long;
const Double = bson.Double;
const Int32 = bson.Int32;
const Decimal128 = bson.Decimal128;
const TypeChecker = require('../');
fork icon121
star icon761
watch icon0

+ 7 other calls in file

334
335
336
337
338
339
340
341
342
  );
});

context('when providing a long', function() {
  it('correctly converts to NumberLong', function() {
    var stringified = parser.stringify({ test: bson.Long.fromNumber(5) });
    assert.equal(stringified, '{test: NumberLong(5)}');
  });
});
fork icon9
star icon35
watch icon0

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
const bson = require("bson");

// create a new Long object with a value of 100
const myLong = bson.Long.fromInt(100);

// print the string representation of the Long object
console.log(myLong.toString());

In this example, we first import the bson package and create a new Long object with a value of 100 using the fromInt method. We then call the toString method on the myLong object to convert it to a string and print it to the console. The output will be 100.

6
7
8
9
10
11
12
13
14
15
16
17
18
import * as define from "./util/define";
import * as msgCoder from "./components/msgCoder";
import { TcpClient } from "./components/tcpClient";
import { errLog, logInfo } from "./LogTS";
const BSON = require('bson');
const Long = BSON.Long;






let version = require('../package.json').version;
fork icon3
star icon8
watch icon1

+ 3 other calls in file

22
23
24
25
26
27
28
29
30
31
32
33
34


module.exports.BSON_INT32_MAX = 0x7fffffff;
module.exports.BSON_INT32_MIN = -0x80000000;


module.exports.BSON_INT64_MAX = BSONJS.Long.MAX_VALUE;
module.exports.BSON_INT64_MIN = BSONJS.Long.MIN_VALUE;


module.exports.JS_INT_MAX = Number.MAX_SAFE_INTEGER;
module.exports.JS_INT_MIN = Number.MIN_SAFE_INTEGER;

fork icon0
star icon0
watch icon0

+ 38 other calls in file

151
152
153
154
155
156
157
158
159
160

it(name, metadata, function () {
  const data = {
    a: 12,
    b: new BSON.Int32(12),
    c: new BSON.Long(12),
    d: new BSON.Double(12),
    e: /[A-Za-z0-9]*/,
    f: new BSON.BSONRegExp('[A-Za-z0-9]*'),
    g: undefined
fork icon0
star icon0
watch icon0

+ 3 other calls in file