您好,我正在尝试检索基于另一个文档的用户女巫的角色,我在用户文档中将此角色的引用作为对象列表
执行此操作时出现错误
List<Document> listRoles = (List<Document>) document.get("listRole");
com.mongodb.DBRef 不能转换为 org.bson.Document
{
"_id" : ObjectId("5a8bf269e1147736b4a7d06c"),
"_class" : "fr.anasys.testsecuritymongodb.models.User",
"username" : "hamou",
"password" : "amroun",
"actived" : true,
"listRole" : [
{
"$ref" : "role",
"$id" : ObjectId("5a8bf269e1147736b4a7d06a")
},
{
"$ref" : "role",
"$id" : ObjectId("5a8bf269e1147736b4a7d06b")
}
]
}
MongoDatabase database = mongoClient.getDatabase("rdproject2");
MongoCollection<Document> collection = database.getCollection("User");
Document document = collection.find(Filters.eq("username",email)).first();
if(document!=null) {
String username = document.getString("username");
String password = document.getString("password");
boolean actived = document.getBoolean("actived");
List<Document> listRoles = (List<Document>) document.get("listRole");
List<RefId> listRefIds = new ArrayList<>();
for (Document refId :listRoles ) {
listRefIds.add(new RefId(refId.getString("ref"),refId.getString("id" )));
}