【发布时间】:2015-09-16 10:25:14
【问题描述】:
我有一个结构化的 Mongo 文档:
{
"_id": value,
"imageShared": {
"imageid": value,
"commentdatadoc": [
{
"whocommented": value,
"commenttext": value,
"commenttimestamp": isodate(111)
},
{
"whocommented": value,
"commenttext": value,
"commenttimestamp": isodate(444)
},
{
"whocommented": value,
"commenttext": value,
"commenttimestamp": isodate(222)
}
]
}
};
这里我想对字段commenttimestamp desc 进行排序。我尝试了下面的方法,但它不起作用......
Query getComments = new Query();
getComments.addCriteria(Criteria.where("imageShared.imageId").is(imageId)).
with(new Sort(Sort.Direction.DESC,"imageShared.commentDataDoc"));
SharedMediaCollec sharedMediaCollec = mongoTemplate.findOne(getComments, SharedMediaCollec.class);
有人知道如何对数组内的文档字段进行排序吗?
【问题讨论】:
-
您正在尝试对一个元素进行排序:
mongoTemplate.findOne将始终返回一个元素。排序应用于一组结果,您告诉 mongoTemplate 按imageShared.commentDataDoc对返回的SharedMediaCollecs 进行排序,而不是对其commentDataDoc进行排序。