简体   繁体   中英

reference value of map type field in mongo using spring data (dbref)

I have a map field in my document (key = content type, value = content) where I want the value part(content, which is another document) to be saved in mongodb as a referenced object.

private Map<ContentType, Content> relatedContents;

For example, for a content type, "CONTENT_TYPE_X", it should save like

"relatedContents" : { "CONTENT_TYPE_X" : DBRef("content", ObjectId("51ea2c0167e855d6b3d3dda3") }

For a normal field reference can be achieved using @DBREF , eg.

@DBRef
private Content content;

But of course it doesn't work in this case by just putting @DBRef , is it somehow possible to achieve what I am saying?

@DBRef
private Map<ContentType, Content> relatedContents;

Many Thanks.

You can do it exactly as you propose with spring-data-mongodb version 1.3 M1 or later:

@DBRef
private Map<String, Content> relatedContents;

see DATAMONGO-657

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM