Hi,
I'm quite new to Mongo & Spring data.
I'm writing a complex but straight-forward data project using spring-data-mongodb (1.0.3.RELEASE)
So far, everything worked more-or-less as expected, until I got stuck with this issue, and had no luck finding any similar working examples on the web.
I have a document nested in a POJO list within another document.
I want to query according to the nested document's ID.
For abstractation, my objects are:
Code:
public class Doc1{
@Id
private String id;
private List<Mapping> mappings;
}
public class Mapping{
private Provider provider;
private String data;
}
public class Provider{
@Id
private String id;
private String name;
}
I get the expected results when querying directly as follows:
Code:
db.doc1.find({ "mappings.provider._id" : ObjectId("50cc34b25cf2ed0193f384ed") })
I tried querying for it through code in all sorts of ways:
- Via a @Query annotation, searching for .id / ._id / .$id, passing to it String, ObjectId or Provider
- Via the Query/Criteria API, passing to it String, ObjectId, BasicDbObject
Currently, I worked around the problem in the following manner:
Code:
public List<Doc1> findByProviderId(String providerId) {
Provider provider = mongoTemplate.findById(providerId, Provider.class);
Criteria criteria = Criteria.where("mappings.provider.name").is(provider.getName());
return mongoTemplate.find(Query.query(criteria), Doc1.class);
}
This works, but I really want to query by the ID, since I don't want to index name, and also there're other fields and the name isn't guaranteed to be unique.
Also, I don't want to use a @DBRef since I want the object's snapshot in its last save stored, and I want to use its fields for queries.
Any help would be greatly appreciated!
ananken