在 Spring Data (Mongodb) 中实现排序的问题

发布于 12-04 20:33 字数 596 浏览 0 评论 0原文

我需要使用 spring data api 执行以下 sql 查询:

Select * from TagTest where tagName = "water temperature" Order by timestamp desc;

我想出了查询:

Query query = new Query(Criteria.where("tagName").is("water temperature"));

我将 Sort 定义为:

query.sort().on("timestamp", Order.DESCENDING);

并且使用 mongoTemplate 执行 findOne() 类似:

mongoTemplate.findOne(Collection, query, MongoTag.class);

但是我找不到应用排序来检索 findOne 中结果的方法。 方法正确吗?如果我错了,请告诉我正确的方法。 谢谢。

I need to do the following sql query using spring data api:

Select * from TagTest where tagName = "water temperature" Order by timestamp desc;

I came up with the query:

Query query = new Query(Criteria.where("tagName").is("water temperature"));

I defined Sort as:

query.sort().on("timestamp", Order.DESCENDING);

and the using mongoTemplate do findOne() like:

mongoTemplate.findOne(Collection, query, MongoTag.class);

But I cannot find a way to apply the sort to retrieve results in findOne.
Is the approach correct? Please let me know the correct approach in case I am wrong.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心2024-12-11 20:33:57

首先,Mongo 不是 SQL 数据库,因此您永远不会在其上运行任何 SQL。我建议花一些时间在 Mongo 命令行上熟悉 Mongo 中文档查询的工作方式 - 它在某些方面与 SQL 非常不同,但在其他方面完全相同。我还阅读了 Mongo 文档,这非常好: http://www.mongodb.org/display/DOCS/Home" rel="nofollow">http://www.mongodb.org/display/DOCS/Home" mongodb.org/display/DOCS/Home

其次,查看您想要的“SQL”查询,看起来您想要所有 TestTags,而不仅仅是一个。 findOne 正如它所说的那样 - 返回一条记录。您可能想使用 find 来代替。

一个好的起点可能是弄清楚您将在 mongo 命令行上运行什么并从那里开始工作。

您不能同时使用 findOne 和排序,但应该能够使用 findAll 和排序。如果您只想要一个结果,您可以为查询添加一个限制:我相信 query.limit(1)

First of all, Mongo isn't a SQL database, so you will never be running any SQL on it. I recommend spending some time on the Mongo command line to familiarise yourself with the way querying for documents works in Mongo - it's very different to SQL in some ways, exactly the same in others. I'd also have a read of the Mongo documentation, which is pretty good: http://www.mongodb.org/display/DOCS/Home.

Secondly, looking at the 'SQL' query you're aiming for, it looks like you want all the TestTags, not just one. findOne does just what it says - returns a single record. You probably want to use find instead.

A good place to start is probably to work out what you'd be running on the mongo command line and work from there.

You can't use findOne and sort together, but you should be able to use findAll and sort together. And if you only want one result, you can add a limit to the query: query.limit(1) I believe.

剩余の解释2024-12-11 20:33:57

为什么要运行 findOne 然后尝试对其进行排序?您在传统 SQL 中尝试过吗?这没有道理。否则你就走在正确的道路上。尝试以下方法,它对我有用:

public List<Person> listPersons() {
    Query query = new Query();
    query.limit(MAX_ROWS)
        .sort().on("dateCreated", Order.ASCENDING);

    return mongoTemplate.find(query, Person.class);
}

Why are you running a findOne and then trying to sort it? Have you tried that in traditional SQL? It does not make sense. Otherwise you are on the right path. Try the following, it worked for me:

public List<Person> listPersons() {
    Query query = new Query();
    query.limit(MAX_ROWS)
        .sort().on("dateCreated", Order.ASCENDING);

    return mongoTemplate.find(query, Person.class);
}
幸福不弃2024-12-11 20:33:57

你可以这样使用它:

public List<Person> listPersons() {
Criteria criteria=Criteria.where("tagName").is("water temperature"));
Query query = new Query();
query.addcriteria(criteria);
query.with(new Sort(Sort.Direction.ASC,"dateCreated")) ;   

return mongoTemplate.find(query, Person.class);
}

我认为这是对你的问题的回答。

You can use it like this :

public List<Person> listPersons() {
Criteria criteria=Criteria.where("tagName").is("water temperature"));
Query query = new Query();
query.addcriteria(criteria);
query.with(new Sort(Sort.Direction.ASC,"dateCreated")) ;   

return mongoTemplate.find(query, Person.class);
}

is the response to your question i think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文