感谢您阅读
我正在引用其他集合中的文档,如下所示:
{"name" : "John", "friends": [
{"$ref": "profile", "$id": ObjectId("5c54f912fdcdd041c8d4232a")},
{"$ref": "profile", "$id": ObjectId("5c641b2c8ca9cebfe874c5c9")}
]}
我的问题是如何获得实际的参考文件?例如,在node.js
和mongoose
中,我们使用populate()
方法。
一个朋友告诉我这是在JAVA中自动发生的,所以除了常规的数据检索以及寻找解决方案外,我没有做任何其他事情。因此,我得到了列表,并且打印尺寸没有问题,但是当我尝试访问引用文档的字段时,出现了空异常错误。
Document playerDoc = this.PLUIGN.getDatabaseHelper().getCollection('friendzone').find(eq("name", "John")).first();
List<Document> friendsList= (List<Document>) playerDoc.get("friends");
friendsList.size(); // returns 2
for(int i = 0; i < friendsList.size(); i++) {
if(friendsList.get(i) == null)
this.PLUGIN.log("Item " + i + " is null");
else
// HERE IS WHERE I GET THE ERROR
this.PLUGIN.log("LORE: " + friendsList.get(i).getString("lore"));
}
谢谢