您好我正在使用mongodb java驱动程序3.0.我有一个文件,如:
db.employee.find().pretty()
"_id" : ObjectId("559e4ae4aed7561c94e565d5"),
"empid" : 1,
"name" : "abc",
"dob" : "17/05/1990",
"address" : [
"559e4ae3aed7561c94e565d3",
"559e4ae4aed7561c94e565d4"
]
Run Code Online (Sandbox Code Playgroud)
我想查询地址字段并获取所有地址(在我的情况下是上面的那些值),而不指定哪个值.我的代码是:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
我不想手动指定地址.如果我查询地址,我应该得到这两个值.有什么建议?
得到了答案,我们可以在其他已知字段上查询例如: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());
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
546 次 |
| 最近记录: |