问题描述
只是想知道什么是最好的方法.以下代码需要能够将字符串日期作为日期值降序排序,但要分页.
Just wondering what the best approach is. The following code needs to be able to sort a string date as a date value in descending order, but with pagination.
documentList = collection.find().skip(skip).limit(limit).sort(Sorts.descending("ReceivedDate"));
除了排序不起作用外,其余的行都可以使用.我仍然习惯于使用Mongo.认为最好找出正确的做事方法.早点养成好习惯.
Aside from the sort not working the rest of the line works a treat. Im still getting used to using Mongo. thought it best to find out the right way, or good way, of doing things. Get into good habbits early.
与我发现的链接的不同之处在于,我在分页中添加了
The difference to the links I found was im adding in pagination.
我查看了一些链接,但不确定100%使用find时需要什么.在Mongo上按日期字符串排序(升序)> MongoDB排序日期字符串(mm/dd/yyyy)
There are a few links I looked at but not 100% certain what I need when I was using find.Sort by date string (ascending) on MongoMongoDB sorting date string (mm/dd/yyyy)
我们试图用降序分页的典型文档是
A typical document we are trying to paginate with descending sort is
{
"_id" : ObjectId("5ddc80b3adbe1d0001bc50f7"),
"ReceivedDate" : "20/12/2019",
"ReceivedTime" : "08:00:00",
"batch_id" : "112233",
"EventMessage" : "SUCCESS",
"Observations" : 1,
"DataSet" : "xxxx",
"SetType" : "yyy",
"SetName" : "yyyxxx",
}
非常感谢,罗素
推荐答案
documentList = collection.find().skip(skip).limit(limit).sort(Sorts.descending("ReceivedDate"));
除了排序不起作用外,其余的行都可以享受.
Aside from the sort not working the rest of the line works a treat.
您必须使用"YYYY-MM-DD"(字符串格式)格式的ReceivedDate
(字符串格式)才能对其进行排序,或使用日期字段,其中包含字符串中的值.
You have to use the ReceivedDate
(string format) in the format of "YYYY-MM-DD" (string format) to be able to sort it, or use a date field with the values from the string.
做到这一点的方法是使用聚合;例如:
The way to do it is use aggregation; for example:
字符串日期为{ _id: 1, dt : "20/12/2019" }
的文档可以转换为Date
进行如下排序:
The document with string date: { _id: 1, dt : "20/12/2019" }
, can be converted to a Date
for sorting like this:
db.test.aggregate( [
{
$project: {
dt: {
$dateFromString: { dateString: "$dt", format: "%d/%m/%Y" }
}
}
},
] )
结果:{ "_id" : 1, "dt" : ISODate("2019-12-20T00:00:00Z") }
并且,其余查询可以是聚合查询,其功能与您现在使用的find
查询相同.以下代码解决了该问题.
And, the rest of the query can be an aggregation query with the same functionality as in the find
query you are using now. The following code solves the issue.
解决方案:
您的查询:
documentList = collection.find().skip(skip).limit(limit).sort(Sorts.descending("ReceivedDate"));
在 Mongo Shell 的聚合中翻译为以下内容:
translates to the following in aggregation in Mongo Shell:
db.test.aggregate( [
{
$project: {
dt: {
$dateFromString: { dateString: "$dt", format: "%d/%m/%Y" }
}
}
},
{ $sort: { dt : -1 } },
{ $skip: 2 },
{ $limit: 1 }
] )
而且,在 Java 中:
Bson addFields = addFields(new Field<Document>("dt",
new Document("$dateFromString",
new Document("dateString", "$dt")
.append("format", "%d/%m/%Y")
)
));
List<Bson> pipeline = Arrays.asList(addFields, sort(descending("dt")), skip(2), limit(1));
List<Document> results = new ArrayList<>();
collection.aggregate(pipeline).into(results);
// The required MongoDB driver imports:
import org.bson.Document;
import org.bson.conversions.Bson;
import static com.mongodb.client.model.Aggregates.addFields;
import static com.mongodb.client.model.Aggregates.limit;
import static com.mongodb.client.model.Aggregates.skip;
import static com.mongodb.client.model.Aggregates.sort;
import static com.mongodb.client.model.Sorts.descending;
import com.mongodb.client.model.Field;
参考文献:
这篇关于Mongo-Java-获取所有文档将字符串日期排序为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!