Search code examples
javamongodbmongodb-java

How to get a String inside an Object Array using MongoDB Java?


I'm trying to learn MongoDB and I want to get a String inside an Object Array, my MongoDB document is here:

{
   "_id" : ObjectId("5f1507fed91e81246c409a59"),
   "identification" : "punishment",
   "lastID" : 2,
   "punishmentsTypes" : [ 
       {
           "category" : "OFENSA_JOGADOR",
           "reason" : "Ofensa a Jogador",
           "group" : [ 
               "HELPER", 
               "MODERATOR", 
               "ADMINISTRATOR", 
               "MANAGER", 
               "MASTER"
           ],
           "description" : "Ofender algum jogador",
           "cases" : [ 
               {
                   "1" : {
                       "type" : "MUTE",
                       "duration" : 604800000
                   }
               }, 
               {
                   "2" : {
                       "type" : "BAN",
                       "duration" : 0
                   }
               }
           ]
       }, 
       {
           "category" : "FALSIFICACAO",
           "reason" : "Falsificação de provas",
           "group" : [ 
               "ADMINISTRATOR", 
               "MANAGER", 
               "MASTER"
           ],
           "description" : "Falsicar provas ao denunciar um jogador em nosso fórum",
           "cases" : [ 
               {
                   "1" : {
                       "type" : "BAN",
                       "duration" : 0
                   }
               }
           ]
       }, 
       {
           "category" : "HACK",
           "reason" : "Hack",
           "group" : [ 
               "MODERATOR", 
               "ADMINISTRATOR", 
               "MANAGER", 
               "MASTER"
           ],
           "description" : "Uso de cheats ou programas ilícitos",
           "cases" : [ 
               {
                   "1" : {
                       "type" : "BAN",
                       "duration" : 7776000000.0
                   }
               }, 
               {
                   "2" : {
                       "type" : "BAN",
                       "duration" : 0
                   }
               }
           ]
       }
   ],
   "unpunishmentTypes" : [ 
       {}
   ]
}

I've tried this:

MongoCollection<Document> collection = mongoDatabase.getCollection("settings");

BasicDBObject query = new BasicDBObject();
BasicDBObject field = new BasicDBObject();

query.put("id", "punish");
field.put("punishmentsTypes", 1);
field.put("_id", 0);

Document test = collection.find(query).projection(field).first();

assert test != null;
Object object = test.get("punishmentsTypes");

System.out.println(object);

And that's the output:

[Document{{category=OFENSA_JOGADOR}}, Document{{category=FALSIFICACAO}}, Document{{category=HACK}}]

How can I get only the category string, to the output be: OFENSA_JOGADOR, FALSIFICACAO, HACK?


Solution

  • I'm not sure how did you get that result with your query but here is how I get your result:

    
        MongoCollection<Document> collection = mongoDatabase.getCollection("settings");
        Document query = new Document();
        
        query.put("identification", "punishment");
        
        Document test = collection.find(query).first();
        
        List<Document> punishmentsTypes = test.getList("punishmentsTypes", Document.class);
        
        for (Document document : punishmentsTypes) {
            String category = document.getString("category");
            System.out.println(category);
        }