Announcement Announcement Module
Collapse
No announcement yet.
$elemMatch in field.include Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • $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
    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?

    Comment


    • #3
      Hi,

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

      Cheers
      Kim

      Comment


      • #4
        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, 03:48 AM.

        Comment


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

          Comment

          Working...
          X