簡體   English   中英

在mongodb Java驅動程序3.0.x中查找JSON值

[英]Find JSON value in mongodb java driver 3.0.x

例如,我有插入到mongo DB Java驅動程序3.0.4中的這兩個JSON文件。 我正在嘗試根據字段ID獲取詳細信息。 但是我所有的努力都是徒勞的。 誰能告訴我我搞砸了。 提前致謝

Document {
    _id = 5721dd81ef31f82754eac196,
    Details = {
        "author_FirstName" : "Nishant",
        "author_LastName" : "Jayanth",
        "book_title" : "skjfsasfhkj",
        "Id" : 78,
        "publisher" : "tata",
        "year" : 1231,
        "ISBN" : 291939
    }

}

Document {
    _id = 5721dd81ef31f82754eac196,
    Details = {
        "author_FirstName" : "Nishant",
        "author_LastName" : "Jayanth",
        "book_title" : "skjfsasfhkj",
        "Id" : 777,
        "publisher" : "tata",
        "year" : 1231,
        "ISBN" : 291939
    }

}

這是我嘗試根據ID檢索的代碼。

public String getAuthor(String id){
    String pId=String.valueOf(id);
    final String[] author = {null};
    FindIterable<Document> iterable=store.find(new Document("Id", 777)); //Probably this is where something is wrong
    iterable.forEach(new Block<Document>() {
    @Override
    public void apply(final Document document) {
          author[0]=document.toString();
          System.out.println(document);
        }       
    });

        return author[0]; 
    }

據我所知,我嘗試了所有可能的組合。

new Document("Document.Details.Id":id);
new Document("Details.Id":id);
new Document("Document.Id":id);

但是沒有任何效果。

上面的JSON文檔未解析。 解析文檔,並立即使用字段名稱進行訪問。

collection.insertOne(Document.parse(您的數據));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM