简体   繁体   中英

Declaratively wrap array elements with objects in MongoDB/Morphia

I have a legacy DB structure, where every array element is wrapped as a key-value:

"field" : [
  {
    "_id" : ObjectId("123456789012345678901234")
  }, {
    "_id" : ObjectId("123456789012345678901235")
  }
]

Can I model it in Java without introducing an intermediate object? Eg something like

@SomeMagicAnnotation("_id")
List<ObjectId> field;

Morphia will want a class structure to map because of those documents. You can, however, use @PostLoad to iterate over that array and create a new array with only those ObjectId values. Once you have this new array, you can put it back in the DBObject with put("field", newArray) . This will make it look like a bare array of ObjectID values to Morphia who can then map it to your List<ObjectID> .

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