Shivan Dragon
Shivan Dragon

Reputation: 15219

Mongo ReflectionDBObject, map ALL embedded array's elements to a class;

I use Mongo with native Java driver (no 3rd party library/ORM). I have this:

public class Release extends ReflectionDBObject {

    //other fields omitted    

    private List<ReleaseDetailsByTerritory> releaseDetailsByTerritory = new ArrayList<ReleaseDetailsByTerritory>();

}

public class ReleaseDetailsByTerritory extends ReflectionDBObject { //...}

If I want to retrieve a "Release" entry having two "ReleaseDetailsByTerritory" entries, and have them auto-instanciated in a Release class instance containing a List of two ReleaseDetailsByTerritory class instances, I have to do this:

releaseColl.setObjectClass(Release.class);
releaseColl.setInternalClass("ReleaseDetailsByTerritory.0", ReleaseDetailsByTerritory.class);
releaseColl.setInternalClass("ReleaseDetailsByTerritory.1", ReleaseDetailsByTerritory.class);
Release r = (Release) releaseColl.findOne();

i.e. I need to specifically map each potential element of the embedded array to the corresponding class.

Is there a way to tell the Mongo driver that I want all and any element of an embedded array to be mapped to a certain class? Something like :

collection.setInternalClass("ReleaseDetailsByTerritory.*", ReleaseDetailsByTerritory.class);

?

Thanks. And please don't say "use Spring MondoDb module or Morphia". I want to know if this is achievable with the Mongo native Java driver.

Upvotes: 3

Views: 528

Answers (1)

Remon van Vliet
Remon van Vliet

Reputation: 18595

Looking at the source code and I don't think this is possible. There is also no obvious way to create convenience functionality for what you need. Having to call setInternalClass for each array element is hardly an option given the massive amounts of memory usage this would result in for large arrays.

You may want to consider implementing your own implementation of a "Document" class that does what you need without having to go to a full mapping solution such as Morphia (which is actually pretty elegant, more so than Spring at least).

You could also consider opening a JIRA issue at jira.mongodb.org and request this feature.

Upvotes: 2

Related Questions