1

I am working right now with MongoDB and Java in Eclipse. I want to represent coordinates in the API of Google Maps from a JSON document.

I have in the JSON document two attributes,latitude and longitude, both of them are double. But when i do this:

List<Pair<Double, Double>> coordenadas = new ArrayList<Pair<Double, Double>>();
    Pair<Double, Double> coordenadasAirport;
    MongoCursor<Document> cursor = collection.find().iterator();
    try {
        while (cursor.hasNext()) {
            Double latitude = cursor.next().getDouble("latitude").doubleValue();
            Double longitude = cursor.next().getDouble("longitude").doubleValue();

The second one, longitude, give me this error:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

How to solve this?

RESOLVED:

Thanks for try to help me. Finally i solved this problem. The problem was the import because when i do the import of the JSON to the database of MongoDB this database assigns the type of data that the DB wants so some data of latitude and longitude have the type Int32 and other data have the type Double.

Regards.

2
  • cursor.next().getDouble("latitude").doubleValue(); return doule or int ? i guess it returns double Commented May 2, 2018 at 16:33
  • 1
    As noted, the data is clearly of an Integer type in storage. That said, it clearly should not be for location data and it's also really not that useful from the MongoDB perspective to be stored in separate document properties. If you expect the database to do anything useful with this data at any time, you best update the stored documents to either of the storage forms that are actually supported. See Geospatial Queries in the core documentation Commented May 2, 2018 at 21:30

1 Answer 1

1

The documentation of Document.getDouble(String) says:

Throws: ClassCastException - if the value is not an double

The value is apparently an Integer, so use getInteger instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.