잡동사니

반응형

질문

MongoDB를 배우려고하는데 객체 배열 내부에 문자열을 넣고 싶습니다. MongoDB 문서는 다음과 같습니다.

{
   "_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" : [ 
       {}
   ]
}

저는 이 코드를 시도하였습니다 :

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);

그리고 그것이 출력입니다.

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

카테고리 문자열 만 OFENSA_JOGADOR, FALSIFICACAO, HACK? 로 출력하려면 어떻게해야합니까?

 

답변1

쿼리로 결과를 어떻게 얻었는지 잘 모르겠지만 결과를 얻는 방법은 다음과 같습니다.


    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);
    }



 

 

 

 

출처 : https://stackoverflow.com/questions/63026549/how-to-get-a-string-inside-an-object-array-using-mongodb-java

반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band