MongoDB:通过@DBRef查询 [英] MongoDB: query by @DBRef

查看:730
本文介绍了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> {
}

    ...

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

@Document
public class WrappedA {
   @Id
   private String id;
   @DBRef
   private JetAnotherClass referenced;
   ...
}

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

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") }} )

有帮助吗? 预先感谢!

解决方案

由于您看起来只通过_id查询,所以我相信您可以做到:

db.NotificationA.findOne({"tag.$id": ObjectId("blah")});

但是:

但是我需要查询JetAnotherClass的字段(两个级别的@DBRef字段).

DBRef不是联接,它们只是描述_id的自我,如果您不知道它将创建帮助对象的链接集合,那么您不必自己在客户端编写此代码. /p>

您可以在此处找到有关DBRef的更多信息: http://docs.mongodb.org/manual/applications/数据库引用/

基本上,您可以从同一文档中查询DBRef中的子字段,即:DBRef.$_id,但是您不能在服务器端解析该DBRef并查询结果字段.

I have a class hierarchy designed for store user notifications:

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

    ...

This is useful for returning polymorphic arrays, allowing me to store any kind of data in the "tag" field. The problem starts when the wrapped objects contains @DBRef fields:

@Document
public class WrappedA {
   @Id
   private String id;
   @DBRef
   private JetAnotherClass referenced;
   ...
}

Queries on the fields of "tag" works fine:

db.NotificationA.find( {"tag.$id": ObjectId("507b9902...32a")} )

But I need to query on the fields of JetAnotherClass (two levels of @DBRef fields). I've tried with dot notation and also with subobjects but it returns null:

Dot notation:

db.NotificationA.findOne( {"tag.$referenced.$id": ObjectId("508a7701...29f")} )

Subobjects:

db.NotificationA.findOne( {"tag.$referenced": { "_id": ObjectId("508a7701...29f") }} )

Any help? Thanks in advance!

解决方案

Since you look like you are only querying by _id I believe you can do:

db.NotificationA.findOne({"tag.$id": ObjectId("blah")});

However:

But I need to query on the fields of JetAnotherClass (two levels of @DBRef fields).

DBRefs are not JOINs, they are merely a self describing _id in the event that you do not know the linking collection it will create a helper object so you don't have to code this yourself on the client side.

You can find more on DBRefs here: http://docs.mongodb.org/manual/applications/database-references/

Basically you can query the sub fields within the DBRef from the same document, i.e.: DBRef.$_id but you cannot, server-side, resolve that DBRef and query on the resulting fields.

这篇关于MongoDB:通过@DBRef查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆