0

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);
            }

2 Answers 2

1

Simply add annotation to your method :

public interface TestRepository extends MongoRepository<mytest, String> {
        @Query("{'data.type.testId': ?0}")
        List<data> findBytestId(String string);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks its working , I have one doubt regarding mongo pojo class of mytest i.e if i set other field name with MsgId in mongo pojo class, when i try to find that value getting no property found on mytest class, if i changed to msgId then i am getting value.what is the reason for this? for example { "MsgId": "not getting value", "data" : { "type" : [ ] }}
it probably comes from Spring, which needs bean fields to have particular syntax (minus character as first, camelCase syntax, etc...)
0

findBytestId => "testId" must be "TestId"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.