Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Spring Data MongoDB在查询中定位数组的最后一项?_Spring_Mongodb_Spring Data Mongodb - Fatal编程技术网

如何使用Spring Data MongoDB在查询中定位数组的最后一项?

如何使用Spring Data MongoDB在查询中定位数组的最后一项?,spring,mongodb,spring-data-mongodb,Spring,Mongodb,Spring Data Mongodb,想象一个博客帖子的集合,每个帖子都有一系列的评论。注释数组中的最后一条注释始终是最新的 { subject: "This is my post", comments: [ {"priority": "HIGH", "text": "Hello, my name is...", "createdAt": ISODate("2021-01-18T09:51:

想象一个博客帖子的集合,每个帖子都有一系列的评论。注释数组中的最后一条注释始终是最新的

{
  subject: "This is my post",
  comments: [
   {"priority": "HIGH", "text": "Hello, my name is...", "createdAt": ISODate("2021-01-18T09:51:32.000Z")},
   {"priority": "MEDIUM", "text": "I agree", "createdAt": ISODate("2021-01-19T09:51:32.000Z")},
   {"priority": "LOW", "text": "Nice post", "createdAt": ISODate("2021-01-20T09:51:32.000Z")}
 ]
}
我想创建一个查询,其中显示:

给我所有最新评论优先的博客帖子 “高”

我所尝试的:
Query Query=new Query();
List criteriaList=新建ArrayList();
//其他过滤器添加到此处的条件列表中
CommentPriorityEnum=CommentPriority.valueOf(“高”);
criteriaList.add(Criteria.where(“comments.-1.priority”).is(priorityEnum);
query.addCriteria(新条件().andOperator(criteriaList.toArray(新条件[criteriaList.size()]));
List posts=mongoOperations.find(查询,BlogPost.class);
这将返回一个空数组作为结果,即使集合中有最后一条评论具有高优先级的博客文章

        Query query = new Query();
        List<Criteria> criteriaList = new ArrayList<>();
        // other filters added to the criteria list here
        CommentPriority priorityEnum = CommentPriority.valueOf("HIGH");
        criteriaList.add(Criteria.where("comments.priority").is(priorityEnum);
        query.addCriteria(new Criteria().andOperator(criteriaList.toArray(new Criteria[criteriaList.size()])));
        List<BlogPost> posts = mongoOperations.find(query, BlogPost.class);
Query Query=new Query();
List criteriaList=新建ArrayList();
//其他过滤器添加到此处的条件列表中
CommentPriorityEnum=CommentPriority.valueOf(“高”);
criteriaList.add(Criteria.where(“comments.priority”).is(priorityEnum);
query.addCriteria(新条件().andOperator(criteriaList.toArray(新条件[criteriaList.size()]));
List posts=mongoOperations.find(查询,BlogPost.class);
这将返回至少有一条优先级为高的评论的所有博客文章,这不是我想要的


有什么想法吗?

点表示法中的负数位置不是MongoDB的一个文档特性


在添加评论时,不要查看数组,而是将评论中的最新评论优先级提升到帖子-在帖子上设置它。然后,您只需通过“帖子上的最新评论优先级=高”进行查询即可无需进入数组。

您可以尝试使用投影操作符。条件是什么?您将如何在Spring数据中使用它?
Criteria
类有一个
elemMatch
方法来查询数组字段。
query
类有可用于投影的
字段
类数组元素。这里有两个方面-数组上的查询过滤器和数组元素的投影。是的,这就是我最后做的。我在BlogPost模型中创建了一个字段lastComment。谢谢
        Query query = new Query();
        List<Criteria> criteriaList = new ArrayList<>();
        // other filters added to the criteria list here
        CommentPriority priorityEnum = CommentPriority.valueOf("HIGH");
        criteriaList.add(Criteria.where("comments.priority").is(priorityEnum);
        query.addCriteria(new Criteria().andOperator(criteriaList.toArray(new Criteria[criteriaList.size()])));
        List<BlogPost> posts = mongoOperations.find(query, BlogPost.class);