Saving a document with large integer values

Heewwoo,

I have a problem when I’m creating a new document and saving it to the database, it’s saving the document with wrong data even if the data collected is a specific value set to. I used Mongodb for about 2 years but this is the first time it’s happening to me.
Here is the data that should be stored in the database:
image
The problem appears on “roleID” which it should be just like in picture, “565193939435388948” (This picture is the data collected, not the data from document inserted.).
And below this is the information that I receive when the document is inserted in database:

image
As you can see, in the picture, on variable “RoleID”, there is not the same id as in the first picture of the data collected.
(Note: In case anyone says the roleID isn’t string as above, it’s because I took the second picture after tried last time to see if it may work after being converted to number but it doesn’t and just left it like that. being string or number doesn’t change the fact that it’s not saving the data correctly as it was collected.)

Practically, from my research, if the number is bigger than 16 characters long, the last 2 characters are like set to 0. I tried to insert a document with 16 characters, it was inserted with the exact data collected. If it’s 17 or 18, the last 2 digits are set to 0.
This is how the code looks like:

As well as the Schema form used:
image

I have used mongodb these 2 years only on windows 7. I have updated recently my pc to windows 10 (not personal choice unfortunately…) and I got this problem. Is because of windows 10?

Just to make sure, I’ll say again:
Windows version: Windows 10
Mongodb server version: 4.2.8

Can someone guess what is going on? I really like mongodb and I don’t wanna give up on it after I spent so much time learning it.

I think it is more related to a javascript,json limitation.

Since you get it as a string (like first image), you could change the mongoose schema to a string. Since it is an id you probably do not make any mathematical computation on it. A string would be fine.

I tried as well to save it as a string but it’s still not working despite chaning mongoose schema to a string.
image

EDIT: If I try to parse the string and treat it as json just like in the link you sent when adding to database, can this actually help it ?
Edit 1.5: Tried to parse and stringify but no use… Isn’t there a way to like change how json works for mongodb?

EDIT 2: I have seen something and Visual studio code gave me a “fix” to make it actually show in database the exact number for the id but that’s working only if I have like the number defined already in a variable and adding an “n” to the end meaning it “covert to bigint numeric literal” but how I can do it with a variable name?

My understanding is that it has to be a string all the way. Otherwise as soon at it is jsonyfied as a number the issue will happen.

What do you mean by

it has to be a string all the way

I have changed it to be A string when collected data and modify in the schema the type to be a string.
what else I’m missing?

EDIT: There isn’t a way to change this in the mongod.cfg file?

The file mongod.cfg has nothing to do with that.

It is really hard to tell from my end. May be an API in your application is still using it as a number.

Can I do something to help you

Hi @Mirage_Zoe,

As @steevej pointed out, the problem is that you are trying to save a numeric value larger than what can be safely represented in a JavaScript Number (aka Number.MAX_SAFE_INTEGER).

If your Role_ID value is an identifier that does not need mathematical manipulation, String would be a more appropriate type to use.

If you have changed Role_ID to a String in your Mongoose schema and are still seeing Number values for newly created documents, I suggest you review your code for any operations which manipulate the Role_Id value and could implicitly coerce the value into a Number.

The Number data type is defined by the JavaScript standard, so this outcome is unrelated to your O/S upgrade or MongoDB server configuration. Manipulation of data types is a client-side issue that you have to resolve in your application code.

I suggest posting this as a Mongoose question on Stack Overflow to get further suggestions. It would be helpful to include your specific versions of Mongoose and Node for context.

Regards,
Stennie

1 Like