如何在MongoDB Java中使用对象数组获取字符串?

huangapple go评论87阅读模式
英文:

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

问题

我试图学习 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?

英文:

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

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

I've tried this:

MongoCollection&lt;Document&gt; collection = mongoDatabase.getCollection(&quot;settings&quot;);
BasicDBObject query = new BasicDBObject();
BasicDBObject field = new BasicDBObject();
query.put(&quot;id&quot;, &quot;punish&quot;);
field.put(&quot;punishmentsTypes&quot;, 1);
field.put(&quot;_id&quot;, 0);
Document test = collection.find(query).projection(field).first();
assert test != null;
Object object = test.get(&quot;punishmentsTypes&quot;);
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?

答案1

得分: 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);
}
英文:

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


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

huangapple
  • 本文由 发表于 2020年7月22日 11:49:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63026549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定