What's the equivalent of NumberInt() from the mongo shell in client JavaScript?

Viewed 2021

In the MongoDB shell, I can insert an integer value of 1 by using the NumberInt(1) type coercion.

How can I do that from client JavaScript that uses the mongodb module?

2 Answers

The Node.js MongoDB driver API exposes three integer types:

Choose the one that best fits your data range and use it like this:

import MongoClient from 'mongodb';
//...
MongoClient.Int32(1)
MongoClient.Long(2)
MongoClient.Decimal128(3)
var NumberInt = require("mongodb").Int32;
var dataToInsert = NumberInt(1)

Now this can be used wherever you would want to insert a value as Int.

Related