Results 1 to 1 of 1

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

Threaded View

  1. #1
    Join Date
    May 2011
    Posts
    16

    Default 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 at 09:16 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •