简体   繁体   中英

MongoDB retrieve entry java

I hava a MongoDB, that looks like this:

{
"_id" : ObjectId("57b16601494957499ae07619"),
"URL" : "some url",
"Text" : "some text ",
"Timestamp" : ISODate("2016-08-15T01:07:00.000Z"),
"RelatedActiveTopics" : [ 
    "apple"
],
"Language" : "en",
"PlatformType" : "NewsAggregator",
"Platform" : "Bing",
"SentimentScore" : 0.0,
"Source" : "Times Publishing Group",
"RelatedDomains" : [ 
    {
        "DomainName" : "Energie",
        "Confidence" : 1
    }

So far my code looks like this:

while (cursor.hasNext()) {
            BasicDBObject dom = (BasicDBObject) cursor.next();
            BasicDBList relatedD = (BasicDBList) dom.get("RelatedDomains");

            List<String> res = new ArrayList<String>();

            DBObject dbObject = (DBObject) dom.get("RelatedDomains");

This gives me as output the DomainName and Confidence, but I only need the DomainName, how can I get just the DomainName?

只需调用dbObject.get("DomainName")

You can try java 8 stream to output a list of DomainName values.

List<String> res = relatedD.stream().
       map(item -> (String)((DBObject)item).get("DomainName")).
       collect(Collectors.toList());

Using for loop

for(Object item : relatedD) {
  res.add((String)((DBObject)item).get("DomainName"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM