Difference between NumberLong(x) and NumberLong(“x”) in MongoDB?


The NumberLong(x) goes beyond its limit value and round off the value whereas NumberLong(“x”) does not.

Now, we will consider a number and would use it for both NumberLong(x) and NumberLong(“x”) to see the difference.

Let us create a collection with documents −

> db.demo603.insert({"longValue" : NumberLong(988998985857575789)});
WriteResult({ "nInserted" : 1 })
> db.demo603.insert({"longValueInString" : NumberLong("988998985857575789")});

Display all documents from a collection with the help of find() method −

> db.demo603.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5e9605e5ed011c280a0905d1"),
   "longValue" : NumberLong("988998985857575808")
}
{
   "_id" : ObjectId("5e9605faed011c280a0905d2"),
   "longValueInString" : NumberLong("988998985857575789")
}

Updated on: 15-May-2020

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements