Итерировать вложенный массив Mon go Document весной с помощью dbCursor и назначить его модели - PullRequest
0 голосов
/ 14 июля 2020

Необходимо перебрать местоположение и сохранить в java POJO, используя Spring MongoDB. Пытался смоделировать список локаций в объект java. Место будет огромным, поэтому я не использую JPA для доступа. Требуются данные о местоположении по частям.

{'parent':'Hello StackOverflow', location:[{'name': 'Location 1', 'type': 'DIGITAL'}, {'name': 'Location 2', 'type': 'DIGITAL'}], }



DBObject query = new BasicDBObject(); //setup the query criteria
        query.put("locationCount", (new BasicDBObject("$eq", new Long(5000))));

        DBObject fields = new BasicDBObject(); 
        fields.put("_id", 0);
        fields.put("location", 1);

        DBCursor dbCursor = mongoTemplate.getCollection("myloccollection").find(query, fields);

        List<BasicDBList> locList = new ArrayList<>();
        while (dbCursor.hasNext()){
             DBObject db = dbCursor.next();
            //BasicDBList studentsList = (BasicDBList) db; // This throws class cast exception
            locList.add(mongoTemplate.getConverter().read(BasicDBList.class, db)); // This provides only one value
}
...