小编Roi*_*Roi的帖子

MongoDB:@DBRef查询

我有一个为商店用户通知设计的类层次结构:

@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> {
}

    ...
Run Code Online (Sandbox Code Playgroud)

这对于返回多态数组很有用,允许我在"tag"字段中存储任何类型的数据.当包装的对象包含@DBRef字段时,问题开始:

@Document
public class WrappedA {
   @Id
   private String id;
   @DBRef
   private JetAnotherClass referenced;
   ...
}
Run Code Online (Sandbox Code Playgroud)

对"tag"字段的查询工作正常:

db.NotificationA.find( {"tag.$id": ObjectId("507b9902...32a")} )
Run Code Online (Sandbox Code Playgroud)

但我需要查询JetAnotherClass的字段(两个级别的@DBRef字段).我尝试过点符号和子对象,但它返回null:

点符号:

db.NotificationA.findOne( {"tag.$referenced.$id": ObjectId("508a7701...29f")} )
Run Code Online (Sandbox Code Playgroud)

子对象:

db.NotificationA.findOne( {"tag.$referenced": { "_id": ObjectId("508a7701...29f") }} )
Run Code Online (Sandbox Code Playgroud)

有帮助吗?提前致谢!

mongodb dbref

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

dbref ×1

mongodb ×1