[英]Find JSON value in mongodb java driver 3.0.x
For instance I am having these two JSON files which I inserted in to mongo DB java driver 3.0.4. 例如,我有插入到mongo DB Java驱动程序3.0.4中的这两个JSON文件。 I am trying to fetch details based on the field Id.
我正在尝试根据字段ID获取详细信息。 But all my efforts are under vain.
但是我所有的努力都是徒劳的。 Can anyone tell where I messed up.
谁能告诉我我搞砸了。 Thanks in Advance
提前致谢
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
}
}
This is my code where I am trying to retrieve based on Id. 这是我尝试根据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];
}
I tried all the possible combinations to the best of my knowledge. 据我所知,我尝试了所有可能的组合。
new Document("Document.Details.Id":id);
new Document("Details.Id":id);
new Document("Document.Id":id);
But nothing worked. 但是没有任何效果。
The above JSON doc is not parsed . 上面的JSON文档未解析。 Parse the document and straight away access with field name.
解析文档,并立即使用字段名称进行访问。
collection.insertOne(Document.parse(your data)); collection.insertOne(Document.parse(您的数据));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.