#Changing the field type

8 messages · Page 1 of 1 (latest)

ivory fable
#

I'm using remix + mongodb + prisma.
We have a field whose value is always a number. For some unknown reason it has been assigned a type of string. There are around 5000 collections in it.

Now I want to modify the field type to number, so that we can do proper comparision in our logic. For example, if we want to delete all the records which are greater than 3450. We can not do it if the field is a string or can we?? No?

ivory fable
#

@deft shard can you please help me with this.

deft shard
#

If you want to perform numerical comparisons like greater than or less than, then it's best to convert the field type from string to Number.
You can do comparison with strings, but it would lead to unexpected behaviour.

Example: In a string comparison, "3450" would be considered greater than "10000" because the comparison is done character by character.

ivory fable
#

Ya. That's the exact issue.

I want to change it. But does it also properly change the old values saved in the database?

#

Because we already have a lot of data in the database. And for all those collections the field has been of type string. I want to change it to number now.

Is it possible?

ivory fable
#

I'm afraid if I change the type now, it'll errase the already present data in the database?? @deft shard

ivory fable
#

And unfortunately the field is _id 😦
_id is assigned a string value. And I want to change it to a number, without loosing the existing data in the database.

deft shard
#

I would suggest you to not directly change the field type; you can create a new field in your documents which would represent the numerical representation of string type. once you are confident about the change and can ensure that the numerical interpretation is correct, then you can delete the string field.

If I understand correctly, do you want to change the type of _id field that is automatically created by MongoDB from string to number?