我有一个为存储用户通知而设计的类层次结构:
@Document
public class Notification<T> {
@Id
private String id;
@DBRef
private T tag;
...
}
@Document
public class NotificationA extends Notification<WrappedA> {
}
@Document
public class NotificationB extends Notification<WrappedB> {
}
...这对于返回多态数组很有用,它允许我在"tag“字段中存储任何类型的数据。当包装的对象包含@DBRef字段时,问题就开始了:
@Document
public class WrappedA {
@Id
private String id;
@DBRef
private JetAnotherClass referenced;
...
}对"tag“字段的查询可以正常工作:
db.NotificationA.find( {"tag.$id": ObjectId("507b9902...32a")} )但是我需要查询JetAnotherClass的字段(两级@DBRef字段)。我尝试过使用点符号和子对象,但它返回null:
点符号:
db.NotificationA.findOne( {"tag.$referenced.$id": ObjectId("508a7701...29f")} )子对象:
db.NotificationA.findOne( {"tag.$referenced": { "_id": ObjectId("508a7701...29f") }} )有什么帮助吗?提前感谢!
发布于 2012-11-13 21:45:42
由于您看起来只是通过_id查询,所以我相信您可以这样做:
db.NotificationA.findOne({"tag.$id": ObjectId("blah")});但是:
,但我需要查询JetAnotherClass的字段(两级@DBRef字段)。
DBRefs不是JOIN,它们只是一个自我描述的_id,在你不知道链接集合的情况下,它将创建一个助手对象,所以你不必自己在客户端编写代码。
你可以在DBRefs上找到更多信息:http://docs.mongodb.org/manual/applications/database-references/
基本上,您可以从同一文档中查询DBRef中的子字段,即:DBRef.$_id,但是您不能在服务器端解析该DBRef并查询结果字段。
https://stackoverflow.com/questions/13361278
复制相似问题