Results 1 to 5 of 5

Thread: $elemMatch in field.include

  1. #1

    Default $elemMatch in field.include

    Does Spring Data support the elemMatch projections?

    http://docs.mongodb.org/manual/reference/projection/elemMatch/


    Using the query.fields.include() it does not appear to work, since this method treats every field as a <field>:1 in the underlying mongo query. What I really want is a

    { _id: 0, dependents: { $elemMatch: { school: 102 }}};

    Can anyone let me know if this is possible with Spring Data or even the Java Driver?

  2. #2
    Join Date
    Sep 2011
    Posts
    5

    Default anyone?

    We're trying to do the same thing. I've found a forum saying using the @Query annotation someone got it working, but is there a way to do this through the Query classes?

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Default

    Hi,

    Did you finally find a way to do it with Spring Data?

    Cheers
    Kim

  4. #4
    Join Date
    Mar 2013
    Posts
    1

    Default

    I had similar requirement. Following worked for me. Finding this was a nightmare.

    Query q1 = new Query(Criteria.where("topElementId").is("B00121")) ;
    Query q2 = new Query(Criteria.where("innerArrayElement").elemMatc h(Criteria.where("_id").is(new ObjectId(someId))));
    BasicQuery bsq = new BasicQuery(q1.getQueryObject() , q2.getQueryObject());
    template.findOne(bsq, TopClassName.class);

    The _id can be replaced with any element from the innerArrayElement.



    ------------------------
    Following is the mongo and spring data sample.

    db.schools.find( { zipcode: 63109 },
    { students: { $elemMatch: { school: 102 } } } )


    Query q1 = new Query(Criteria.where("zipcode").is("63109"));
    Query q2 = new Query(Criteria.where("students").elemMatch(Criteri a.where("school").is("102")));
    BasicQuery bsq = new BasicQuery(q1.getQueryObject() , q2.getQueryObject());
    template.findOne(bsq, SchoolList.class);
    Last edited by mevric75; Mar 25th, 2013 at 02:48 AM.

  5. #5
    Join Date
    Sep 2011
    Posts
    5

    Default

    The answer given by mevric75 is exactly what we ended up doing as well.

Posting Permissions

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