How to get a number array from MongoDB to Java class with POJO?

26 Views Asked by At

So basically, I'm trying to get loc value from mongodb like this

{
    _id: '01050',
    city: 'HUNTINGTON',
    loc: [ -72.873341, 42.265301 ],
    pop: 2084,
    state: 'VN'
  }

But when I try to map with float array by using POJO library, it's always show error Failed to decode 'Zip'. Decoding 'loc' errored with: Can't find a codec for CodecCacheKey{clazz=class [D, types=null}.

I tried to use float[], double[], List<Float>() but it's always failed. Here is my code...

public class MongoConnection {
    private static final String CONNECTION_STRING = "mongodb://localhost:27017/";
    private static String DB_NAME = "BikeStores"; // "demodb";

    private static MongoClient client;
    private static MongoDatabase db;

    public static void connectToMongo() {
        connectToMongo(DB_NAME);
    }

    public static void connectToMongo(String databaseName) {
        DB_NAME = databaseName;
        ConnectionString connectionString = new ConnectionString(CONNECTION_STRING);
        client = MongoClients.create(connectionString);
        db = client.getDatabase(DB_NAME);
    }
    
    public static MongoClient getClient() {
        return client;
    }

    public static MongoDatabase getDatabase() {
        return db;
    }
    public static void closeClient() {
        client.close();
    }
}

public class Zip {
    @BsonId
    private String id;
    @BsonProperty("city")
    private String city;
    @BsonProperty("loc")
    private double[]loc;
    
    @BsonProperty("pop")
    private int pop;
    @BsonProperty("state")
    private String state;
    
    public Zip(String id, String city, double[] loc, int pop, String state) {
        super();
        this.id = id;
        this.city = city;
        this.loc = loc;
        this.pop = pop;
        this.state = state;
    }
    
    @BsonCreator
    public Zip() {
        super();
    }

    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    public String getCity() {
        return city;
    }
    
    public void setCity(String city) {
        this.city = city;
    }
    public double[] getLoc() {
        return loc;
    }
    
    public void setLoc(double[] loc) {
        this.loc = loc;
    }
    public int getPop() {
        return pop;
    }
    
    public void setPop(int pop) {
        this.pop = pop;
    }
    public String getState() {
        return state;
    }
    
    public void setState(String state) {
        this.state = state;
    }
    @Override
    public String toString() {
        return "Zip [id=" + id + ", city=" + city + ", loc=" + Arrays.toString(loc) + ", pop=" + pop + ", state="
                + state + "]";
    }
    
}


import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;

public class ZipPOJODAO {
    private static MongoCollection<Zip> zipCollection;
    public static List<Zip> getZipFromMongo(int n, int k) {
        CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build();
        CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(),
                fromProviders(pojoCodecProvider));
//      connect
        MongoConnection.connectToMongo("demodb");
        MongoDatabase db = MongoConnection
                .getDatabase()
                .withCodecRegistry(pojoCodecRegistry);
        
        zipCollection = db.getCollection("zips",Zip.class);
        List<Zip> res = new ArrayList<>();
        zipCollection.find().skip(k).limit(n).into(res);
        return res;
    }
}

public class Execute {
    public static void main(String[] args) {
        
        List<Zip> zipList = ZipPOJODAO.getZipFromMongo(4,5);
        System.out.println(zipList);
        MongoConnection.closeClient();
    }
    
}

I just want to know is there any way to get number from array with POJO, even number. I'm very thank you if someone have an answer <3

0

There are 0 best solutions below