Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

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?

like image 252
Dan Dascalescu Avatar asked Oct 22 '25 17:10

Dan Dascalescu


2 Answers

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

  • Int32
  • Long (64-bit)
  • Decimal128

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)
like image 59
Dan Dascalescu Avatar answered Oct 24 '25 17:10

Dan Dascalescu


var NumberInt = require("mongodb").Int32;
var dataToInsert = NumberInt(1)

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

like image 24
Anurag Kumar Avatar answered Oct 24 '25 17:10

Anurag Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!