Mongodb java driver 3.0 query
Hi i am working with mongodb java driver 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 for address fields and get all addresses (in my case these values ββare above) without specifying what value. My code:
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 enter the address manually. If I ask for an address, I have to get these two values. Any suggestions?
+3
Shaik mujahid ali
source
to share
1 answer
After receiving the response, we can query for another known field like: empid and instead of returning the entire document, return only the required field (in my case).
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());
}
});
+6
Shaik mujahid ali
source
to share