我有一个为商店用户通知设计的类层次结构:
@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 的规范化数据模型结构中收到以下错误:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.DBRef
Run Code Online (Sandbox Code Playgroud)
这是由这一行引起的:
System.out.println(document.toJson());
Run Code Online (Sandbox Code Playgroud)
具体的toJson()部分。我的文档中有一个 DBRef 对象,因此我可以引用另一个集合中的文档。嵌入式文档结构不是选项。那么我该如何解决这个问题?
我在使用 MongoDB 时遇到过几种需要使用 DBRefs 的情况。但是,我还想在 DBRef 本身中缓存引用文档中的一些字段。
{$ref:'user', $id:'10285102912A', username:'Soviut'}
Run Code Online (Sandbox Code Playgroud)
例如,即使引用了用户文档,我也可能希望用户名可用。这将为我提供单一文档方法的所有好处;更快的查询并消除了在我的代码中手动取消引用的需要。同时允许我在有意义的地方使用引用。
这个想法是,当引用的文档更新时(例如,用户更改了他们的名字),我的业务层可以自动更新引用它的所有文档。
最后,我想知道在我的 DBRef 上存储其他字段是否被认为是一种好形式?它会破坏任何东西吗?每次重写参考文献时我都会丢失数据吗?像 pymongo 这样的驱动程序会支持它吗?
我目前正在使用GA版本的Spring Data MongoDB框架,而@DbRef并没有在spring数据mongodb中自动保存子对象.你能告诉我怎样才能让它发挥作用?
我正在使用 spring-data-mongodb 和 querydsl-mongodb 来执行更灵活的查询。
我的应用程序有用户和订单。一个用户可以有多个订单,所以我的模型如下所示:
public class User {
@Id
private String id;
private String username;
//getters and setters
}
public class Order {
@Id
private String id;
@DBRef
private User user;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,用户和订单之间存在多对关系。每个订单都分配给一个用户,该用户存储在@DBRef public User 用户属性中。
现在,假设某个用户有 10,000 个订单。
我如何进行查询以获取属于特定用户的所有订单?
我有订单存储库:
public interface OrderRepository extends MongoRepository<Order, String>,
QueryDslPredicateExecutor<Order> {
}
Run Code Online (Sandbox Code Playgroud)
我尝试了这个解决方案,但它没有返回任何内容:
QOrder order = new QOrder("order");
Pageable pageable = new PageRequest(0, 100);
return userRepository.findAll(order.user.id.eq(anUserId), pageable);
Run Code Online (Sandbox Code Playgroud)
我需要使用 querydsl,因为我想构建一个可以通过比 userid 更多参数查询订单的服务。例如,我想获取属于具有特定用户名的用户的所有订单。
我正在使用 Spring + Spring Data MongoDB。我的模型是这样的:
@Document(collection = "actors")
public class Actor extends DomainEntity {
private String name;
private String surname;
@DBRef(lazy = true)
private List<Class> classes;
Run Code Online (Sandbox Code Playgroud)
另一个类非常通用,所以我不发布它。我的问题是当我尝试访问列表“类”时没有加载它,该属性仍然是某种代理对象。例子:
Actor a = actorRepository.findOne(id);
//At this moment classes are a proxy object because of the lazy
//Now I try to load the reference and nothing works
a.getClasses();
a.getClasses().size();
a.getClases().get(0).getAttr();
for(Class g:a.getClasses()){
g.getAttr();
}
Run Code Online (Sandbox Code Playgroud)
我考虑了很多选择,但没有办法让它工作......
spring lazy-initialization dbref spring-data spring-data-mongodb
是否可以聚合通过 DBRef 存储的数据?
蒙戈2.6
假设我有如下交易数据:
{
_id : ObjectId(...),
user : DBRef("user", ObjectId(...)),
product : DBRef("product", ObjectId(...)),
source : DBRef("website", ObjectId(...)),
quantity : 3,
price : 40.95,
total_price : 122.85,
sold_at : ISODate("2015-07-08T09:09:40.262-0700")
}
Run Code Online (Sandbox Code Playgroud)
诀窍是“source”本质上是多态的 - 它可能是不同的 $ref 值,例如“webpage”、“call_center”等,它们也有不同的 ObjectId。例如,DBRef("webpage", ObjectId("1")) 和 DBRef("webpage",ObjectId("2")) 将是事务发起的两个不同网页。
我想最终按来源在一段时间内(例如一个月)进行汇总:
db.coll.aggregate( { $match : { sold_at : { $gte : start, $lt : end } } },
{ $project : { source : 1, total_price : 1 } },
{ $group : {
_id : …Run Code Online (Sandbox Code Playgroud) 是否可以使用单个查找规范通过 DBRef进行查询?
用户集合
{
'age': 30
}
Run Code Online (Sandbox Code Playgroud)
收集后
{
'user': DBRef('user', ...)
}
Run Code Online (Sandbox Code Playgroud)
是否可以在单个查找步骤中查询所有帖子的用户是谁?如果没有,创建一个javascript函数来处理多阶段操作或者会导致阻塞问题是否明智?
从RDMBS背景来看,很难不考虑像连接这样的思想,特别是在使用无模式的MongoDB环境时.
我在博客上看到DBRefs仅在您知道您引用的对象类型时才有用.
为什么会这样?当然,他们有更多的用途.
假设我有一个用户集合和一个雇主集合.许多用户可以引用同一个雇主.对我来说,这是DBRef的完美使用.但是,这与我在博客上看到的内容相矛盾.
当然,我可以将雇主嵌入到每个用户集合中,但是当雇主改变时会发生什么?也许他们雇主改变地址或电话号码或其他东西.如果雇主嵌入在每个用户中,那么我必须更新每个用户的嵌入式文档.
那不可能有效.或者可以吗?
我一周前开始学习 MongoDB,但我被困在关系上。更像是我很困惑。我知道何时使用嵌入关系以及何时使用引用关系。我知道嵌入关系有一些缺点,这就是为什么我们更喜欢引用关系而不是嵌入关系。现在,我正在学习 DBRef。问题是,我觉得它无论如何都没有帮助。那就是我所想的。我希望我错了。
在 DBrefs 中,我们可以在一个位于不同集合中的文档中引用来自不同集合的文档。在 RefRels 中,我们可以在不同集合中的一个文档中引用不同集合中的不同文档。
我的意思是,我们可以使用 DBref 执行与使用引用关系执行的操作相同的操作。
在引用关系中,我们在文档的集合中创建一个字段,并存储来自不同集合的文档的 ObjectId,如下所示:
> db.Employee.insert({"Emp_Name":"Emp_1", "Emp_Address":[ObjectId("some_id_from_Address_collection"), ObjectId("some_id_from_Address_collection"), ObjectId("some_id_from_Address_collection")], "Emp_Phone":[ObjectId("some_id_from_Phone_collection"), ObjectId("some_id_from_Phone_collection"), ObjectId("some_id_from_Phone_collection")]})
Run Code Online (Sandbox Code Playgroud)
在 DBrefs 中,我们在文档的集合中创建一个字段,并使用 ObjectId 存储值,就像我们过去在引用关系中所做的那样,但方式不同。考虑以下示例:
> db.Employee.insert({"address": {"$ref": "address_home", "$id": ObjectId("534009e4d852427820000002"), "$db": "tutorialspoint"}, "name": "Tom Benzamin"})
Run Code Online (Sandbox Code Playgroud)
我们仍然使用 ObjectId 在 Employee 集合中存储值,但语法不同,因为在此示例中,我们提到要查找哪个数据库和集合。
为什么不直接使用引用关系来节省时间,而不是使用这种令人困惑且冗长的查询来浪费一半的时间?
我错过了什么吗?
我是否应该考虑学习 DBrefs ?