i am working with mongodb java . My document looks like this
_id:ObjectId("abcd1234rf54")
createdDate:"12/11/15"
type:1
nameIdentity:[{"nameOne":"aa","nameTwo":"bb"},
{"nameOne":"aa","nameTwo":"dd"},
{"nameOne":"ee","nameTwo":"ff"}]
where nameIdentity is an array of documents. I am trying to query on nameIdentity if there is an exact match of document for eg {"nameOne":"aa","nameTwo":"dd"}
i should get only {"nameOne":"aa","nameTwo":"dd"}
document as output but i am getting the complete document . My query is
Document fields=new Document("nameOne","aa").append("nameTwo","bb");
Document elemMatch = new Document("$elemMatch", fields);
Document exactElemMatch = new Document();
exactElemMatch.append(MongoConstants.NAME_IDENTITY, elemMatch);
Document resultDocument = mongoDatabase.getCollection(test).find(exactElemMatch).first(); . With this i am getting the whole document but what i want id just the matching criteria i.e., `{"nameOne":"aa","nameTwo":"dd"}`. Where am i going wrong ? And is it possible to get the index of matched document ?