Mongo Scala driver datatype problem.

33 views
Skip to first unread message

Manoj Waikar

unread,
Jan 12, 2017, 3:56:02 AM1/12/17
to mongodb-user
Hi,

I am using mongoimport tool to import data into a few collections. The format of the CSV file is as follows -

Id.int64(),Sid.string(),EmpId.int64(),ReportsTo.int64()
1567688,1,470114,952373
1523614,2,479876,768371
1889728,3,343985,27278
1180413,4,287233,469726

I have a similar CSV file for offices -

Id.int64(),Sid.string(),City.string(),Country.string()
1,1,Georgia,USA
2,2,Pune,India

I am using the Mongo Scala driver 1.2.1. I try to do the following -

val storeId = doc.getInteger("Id").toLong

to retrieve the value of Id field, which corresponds to a Long value (StoreId) in my class.

This code works fine for the offices collection where data is less. However, it fails for hierarchy collection where I am importing 1 million records with this exception -

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
[error]         at org.bson.Document.getInteger(Document.java:151)

I fail to understand two things -

1) When I directly try - val storeId = doc.getLong("Id"), I get this exception -

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
[error]         at org.bson.Document.getInteger(Document.java:151)

So why can't I get the value of the field using getLong, when I've specified in the CSV file that the type of the column has to be int64?

2) How should I get the value of the field? Isn't the behavior odd - sometimes the field is being treated as integer (when there are less number of records - as in offices collection), and sometimes as a long (where there are 1 million records, as in hierarchy collection)?

Thx,
Manoj.

Manoj Waikar

unread,
Jan 12, 2017, 10:32:23 PM1/12/17
to mongodb-user
I am sorry guys, it was my mistake, I was using the wrong database.
So to clarify, the values for int64() fields (imported from CSV) are NumberLong() and doc.getLong() works for those fields.

Manoj.
Reply all
Reply to author
Forward
0 new messages