Sriram
Sriram

Reputation: 101

Nested fields query by using mongo repository in java

i want to get the value of Id by using mongorepository. Below example shows mondodb document

{
    "_id" : ObjectId("5baa4779f4a46b0f60a74313"),
    "_class" : "hello.mytest",
    "data" : {
        "type" : [ 
            {
                "testId" : "Id0",
                "usage" : "near",
                "additionalProperties" : {}
            }, 
            {
                "testId" : "Id1",
                "usage" : "far",
                "additionalProperties" : {}
            }]}
            }

when i try to find testId getting null value.

public interface TestRepository extends MongoRepository<mytest, String> {
        List<data> findBytestId(String string);
            }

Upvotes: 0

Views: 156

Answers (2)

matthPen
matthPen

Reputation: 4363

Simply add annotation to your method :

public interface TestRepository extends MongoRepository<mytest, String> {
        @Query("{'data.type.testId': ?0}")
        List<data> findBytestId(String string);
}

Upvotes: 1

Anjana Thrishakya
Anjana Thrishakya

Reputation: 31

findBytestId => "testId" must be "TestId"

public interface TestRepository extends MongoRepository<mytest, String> {
    List<data> findByTestId(String testId);
}

Upvotes: 0

Related Questions