Announcement Announcement Module
Collapse
No announcement yet.
How to write a query to find by _id which is not at the top level... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to write a query to find by _id which is not at the top level...

    We have a structure which embeds a object called survey with it's own _id. For example:

    Code:
    {
        ...
        ...
        survey {
           _id : 4f595cafda06031df450d8ca
           ...
        }
    
    }
    The type of survey._id is ObjectId as it is stored in Mongo.

    I wrote a finder that looks like this:

    Code:
        @Query(value="{\"survey._id\" : ?0}")
        Page<SurveySnapshot> findBySurveyId(String surveyId, Pageable page, Sort sort);
    Structurally, the class SurveySnapshot contains a member of type Survey called survey which then contains a String of type id:

    Code:
    class SurveySnapshot {
       ...
       private Survey survey
       ...
    }
    
    class Survey {
       private String id;
    }
    But it's not returning anything and the data is there.

    Issuing this query to a MongoQuery tool produces results I need:
    {"survey._id" : ObjectId("4f595cafda06031df450d8ca")}

    However, writing this code:

    Code:
    new BasicQuery("{\"survey._id\" : ObjectId(\"4f595cafda06031df450d8ca\")}")
    Throws a com.mongodb.JSONParseException...

    Am I doing something wrong in constructing the query in java?

    Thanks.
    -AP_
    Last edited by apara; Mar 8th, 2012, 09:16 PM.
Working...
X