suman j
suman j

Reputation: 6960

springdata mongo nested properties query

I have below document corresponding pogo

class Wrapper {
    String id //document Id corresponding field
    String defaultVersion
    String name
    List<VersionedInfo> versions
}

class VersionedInfo {
    String version
    .. few otherproperties
}

As of now to get the document and findout the default version corresponding VersionedInfo, I get the complete document and loop over the versions property list and compare the version with defaultVerion property i.e (Groovy)

Wrapper document = repository.findOne ("id")
VersionedInfo defaultVersionedInfo = document.versions.find { it.version == document.defaultVersion }

Is there a better alternative to query the document to pull only the defaultVersion corresponding VersionedInfo using projections? or QueryDSL?

Upvotes: 1

Views: 587

Answers (2)

suman j
suman j

Reputation: 6960

MongoDB aggregate functionality comes to rescue for this requirement. mongodb $where query to fetch sub-document content has a sample query. Refer to Aggregation Framework Support in Spring data for MongoDB

Upvotes: 0

horacioibrahim
horacioibrahim

Reputation: 156

I'm not sure if below considerations should help you. But help me for defines this rules (similar).

If your application frequently retrieves the VersionedInfo data with the defaultVersion information, then your application needs to issue multiple queries to resolve the references. A more optimal schema would be to embed the VersionedInfo data entities in the defaultVersion (source docs mongoDB). See: http://docs.mongodb.org/manual/tutorial/model-embedded-one-to-many-relationships-between-documents/

Considerations on references: When using references, the growth of the relationships determine where to store the reference. http://docs.mongodb.org/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/

Upvotes: 1

Related Questions