How to map a document to existing array elements in mongodb using java driver

Hi everyone, I'm trying to map a document using the mongodb java driver, for example:

{ "fName" : "abc", "lName" : "456", "dob" : "00", "address" : "xyz" } 

from

 "nameIdentity" : [ { "fName" : "abc", "lName" : "def", "dob" : "00", "address" : "xyz" }, { "fName" : "123", "lName" : "456", "dob" : "00", "address" : "789" } 

If I found a document, then I do nothing to add the document. My problem here is that in my original document there is fname: abc and lname: 456 this corresponds to fname in the first set of nameIdentity and lname in the second set of identifiers. I want this to be one complete coincidence. I tried something like this

 List<Document> nameIdentities = (List<Document>) matchedDocument.get("nameIdentity"); for (int i=0;i<nameIdentities.size();i++) { temp.add(nameIdentities.get(0)); quBasicDBObject=new BasicDBObject("$and",temp); } iterable = mongoDatabase.getCollection("entity").find(updatedDocumentTypeOne); if (iterable.first() == null) { updateResult = mongoDatabase.getCollection("entity") .updateOne( new Document("_id", new ObjectId(objectId)), new Document("$push", new Document("nameIdentity", nameList.get(0)))); } 

any suggestions I'm wrong in?

+4
source share
1 answer

UPDATE You may need to use an aggregation structure.

Maybe something like:

 List<Bson> filterList = new ArrayList<>(); filterList.add(new BsonDocument().append("nameIdentity.fName", new BsonString("abc") )); filterList.add(new BsonDocument().append("nameIdentity.lName", new BsonString("456") )); FindIterable<org.bson.Document> it = collection.find(Filters.and(filterList)); 
+1
source

All Articles