[英]MongoDB Spring Data - Sort by Referenced object property
I search a lot trying to do what I want to do and can't find a solution. 我进行了大量搜索,试图做自己想做的事情,却找不到解决方案。 Can anyone help me? 谁能帮我?
I have two Mongo Collections: 我有两个Mongo Collections:
A. Post 一个帖子
{
content: "...",
user: {
$ref: "user",
$id: ObjectId(...)
}
}
B. User B.用户
{
name: "user name",
age: 21
}
And I want to list all the posts sorted by User name, can I do it? 我想列出按用户名排序的所有帖子,可以吗?
I tried something like that: 我尝试过这样的事情:
Query query = new Query();
query.with(new Sort(new Sort.Order(Sort.Direction.ASC, "user.name")));
return mongoTemplate.find(query, Post.class);
But doesn't work, how can I do? 但是不行,我该怎么办?
MongoDB is only a document store by design, you can use it like a relational DB, if you have to something like this, either MongoDB在设计上仅是一个文档存储,您可以像关系数据库一样使用它,如果必须这样做,
With this schema you can't sort posts by user name in one atomic operation. 使用这种模式,您不能在一个原子操作中按用户名对帖子进行排序。 You have some choices: 您有一些选择:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.