Mongo Repository retrieve query for object with arraylist

641 Views Asked by At

I have the following database query,

db.compDoc.find(
    { "comp_id": "MY00000Y"}, 
    { doc: {$elemMatch: {file: "abc"}}}); 

which gives the result shown below.

{
    "_id" : ObjectId("5bf28dd4cab0a74d4338abf9"),
    "doc" : [
        {
            "desc" : "string",
            "file" : "abc",
            "path" : "string",
            "type" : "U",
            "createdAt" : ISODate("2018-11-21T10:58:40.065+08:00")
        }
    ]
}

I tried to get that result using mongoRepository. I tried the 2 queries shown below.

Query 1:

  @Query(value = "{ 'organizationId' : ?0, 'documents.docName': ?1, 'documents': { $elemMatch: { 'documents.docName' : ?1 } }}")
        Optional<OrganizationDocument> findByOrganizationIdAndDocumentsDocName(String orgId, String docName);

Query 2:

@Query(value = "{ 'organizationId' : ?0, 'documents.docName' : ?1 }", fields = "{ 'documents.docName' : 'abc' }")
    Optional<OrganizationDocument> findByOrganizationIdAndDocumentsDocName(String orgId, String docName);

Results: Get whole document for Query 1 and 2 without filtering.

Please help me to get correct result.

Note : organizationId , documents and docName have matched with database field.

0

There are 0 best solutions below