[英]Mongodb java driver 3.0 query
Hello i am working with mongodb java driver 3.0. 您好我正在使用mongodb java驱动程序3.0。 i have a document like : 我有一个文件,如:
db.employee.find().pretty()
"_id" : ObjectId("559e4ae4aed7561c94e565d5"),
"empid" : 1,
"name" : "abc",
"dob" : "17/05/1990",
"address" : [
"559e4ae3aed7561c94e565d3",
"559e4ae4aed7561c94e565d4"
]
I want to query on address fields and get all the addresses (in my case those values above) without specifying which value. 我想查询地址字段并获取所有地址(在我的情况下是上面的那些值),而不指定哪个值。 My code is : 我的代码是:
FindIterable<Document> iterable = mongoDatabase.getCollection("employee").find(
new Document("address", 559e4ae3aed7561c94e565d3));
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document)
System.out.println(document);
}
});
I don't want to specify the address manually. 我不想手动指定地址。 If I query on the address, I should get those two values. 如果我查询地址,我应该得到这两个值。 Any suggestions? 有什么建议?
Got the answer, we can query on the other known field for eg : empid and instead of returning whole document , only return the field which you require (in my case address). 得到了答案,我们可以查询其他已知字段,例如:empid而不是返回整个文档,只返回您需要的字段(在我的案例地址中)。
FindIterable<Document> iterable = mongoDatabase.getCollection("employee").find(
new Document("empid", 1));
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document.get("address").toString());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.