mwrichardson
mwrichardson

Reputation: 1348

mongoimport - treat CSV "Infinity" string literal as string, not as Double value

I am importing data for a client and have noticed that the string literal "Infinity", when it appears in a CSV file, is treated as a Double value rather than an actual string.

This could make sense when the value was not enclosed in quotes, but I believe in this context it should be treated as a string.

Take the following (simplified) CSV input file:

ID,Name
1,"Infinity"

When imported as follows:

mongoimport.exe -v --host localhost:27017 --username admin --password password --authenticationDatabase admin -d "infinity-test" -c "test-data" --file C:\test-data.csv --type csv --headerline

It yields the following result:

MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
> use infinity-test
switched to db infinity-test
> db.getCollection('test-data').find()
{ "_id" : ObjectId("5a09c48ba7025b2e68885f91"), "ID" : 1, "Name" : Infinity }
>

The issue is easier to spot in MongoBooster where it gives the field type:

enter image description here

Is there a way to force mongoimport to treat the literal "Infinity" as a string?

Upvotes: 1

Views: 441

Answers (1)

kevinadi
kevinadi

Reputation: 13775

Just to elaborate on Neil's comment, in MongoDB 3.4, mongoimport accepts --columnsHaveTypes parameter, which specifies the datatype of the field. By default, it guesses the type based on the content.

The full documentation on possible types is https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-columnshavetypes

For example, you can modify the CSV headerfile to specify that both fields are string instead of numbers:

ID.string(),Name.string()
1,"Infinity"

and import using mongoimport --type=csv --columnsHaveTypes ...

Upvotes: 1

Related Questions