我有一个集合和文档,有相同的字段。但有些字段有不同的类型,比如,"角色 "字段在第一个文档中是一个对象数组,而在第二个文档中是一个常规数组。例如,"Role "字段在第一个文档中是一个对象数组,在第二个文档中是一个常规数组。Morphia如何处理这样的例子?如何从数据库中读取文档?
如果Role字段在所有的文档中都是相同的类型(查询类型),那么就可以从数据库中读取Role字段。Query query = datastore. createQuery (ScheduleData .class); List schedules = query.find().toList();
)效果非常好。如果不同。
error java.lang. RuntimeException: java.lang. ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject
你必须将这些字段声明为 Object
在你的实体中。 Morphia willshould decode in to the correct type before assigning to the field but there's no common parent type of the String
和 Document
以外 Object
.