-
Jan 22nd, 2013, 04:35 PM
#1
$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?
-
Feb 15th, 2013, 01:15 PM
#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?
-
Mar 21st, 2013, 06:46 PM
#3
Hi,
Did you finally find a way to do it with Spring Data?
Cheers
Kim
-
Mar 25th, 2013, 02:43 AM
#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 at 02:48 AM.
-
Apr 2nd, 2013, 11:40 AM
#5
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
-
Forum Rules