Reputation: 699
Am trying to filter a sub-document.
Sample Record:
[Document{{_id=597608aba213742554f537a6, upp_id=, content_id=597608aba213742554f537a3, idmapping=Document{{ptype=PDF, clientid=12345, normalizedclientid=12345, systeminstanceid=, sourceschemaname=, platforminternalid=0987654321}}, batchid=null, locale=en_US}}]
I need to filter using idmapping.ptype = PDF
MongoCursor<Document> cursor = mailboxitemCollection.find(whereClause).iterator();
List<Document> documentList = new ArrayList<Document>();
while (cursor.hasNext()) {
Document object = cursor.next();
documentList.add(object);
}
List<Document> outList = documentList.stream()
.filter(p -> p.getInteger(CommonConstants.VISIBILITY) == 1
&& (!StringUtils.isEmpty(req.ptype())? (p.getString("idmapping.ptype").equalsIgnoreCase(req.ptype())) : true)
).parallel().sequential().collect(Collectors.toCollection(ArrayList::new));
System.out.println(outList);
System.out.println(outList.size());
Am getting Null Point exception, am not able to read the sub/embed document from List documentList.
Thank you in advance! Bharathi
Upvotes: 1
Views: 987
Reputation: 2677
With the mongo-java-driver you can't access the subdocument's fields directly. You should get the subdocument and after that the field of the subdocument like this:
String platformType =
((Document)p.get("idmapping")).getString("ptype");
In your case, change the filter to the following:
.filter(p -> p.getInteger(CommonConstants.VISIBILITY) == 1 && (!StringUtils.isEmpty(req.ptype()) ? (((Document)p.get("idmapping")).getString("ptype").equalsIgnoreCase(req.ptype())) : true))
Upvotes: 5