Arun
Arun

Reputation: 3680

Get data from list of Sub documents in Mongo collection using Spring JPA

Below is the sample of my Mongo-collection data-structure

{
    "id": "5d91fe25da1917111182ce5a",
    "customName": "Chess Application",
    "status":"not_ready",
    "environments": [
        {
        "environmentId": "6bbbbda6-b01a-4b9e-99d5-a1d0f696449a",
        "environmentName": "Dev",
        "environmentType": "dev",

        },
        {
        "environmentId": "3b958d27-8fb7-4edd-bbb0-1dd86437d313",
        "environmentName": "qa",
        "environmentType": "qa",

        }
    ]    
}

Am using spring-JPA to get the data.. I will get only the environmentId as input and i will scan all the collections and get the collection that has this environmentId

Note: the Environment-id here is not mongo-created ID. It is the UUID generated by my Java app during insertion

i used findByEnvironmentsIsIn() method and it is not helpful . Any idea on how to get only one object from the list-of-sub-documents ?

Upvotes: 1

Views: 601

Answers (1)

Achraf Elomari
Achraf Elomari

Reputation: 335

@Query("{'environments' : { $elemMatch: { 'environmentId': { $in: ?0 }}}}")
List<Object> findByEnvironmentsIsIn( Set<String> environmentIds); 

I guess this should work for you

Upvotes: 2

Related Questions