[英]How to get random record from one collection MongoDB
I have entity Article
I'm trying to retrieve one random record from database collection.我有实体
Article
我正在尝试从数据库集合中检索一条随机记录。
This is entity Article
:这是实体
Article
:
@Data
@Document(value = "article")
public class Article {
@Id
private String articleId;
private String title;
private String description;
private String fullArticle;
This is service to save it:这是保存它的服务:
@Override
public Article save(Article article) {
return articleRepository.save(article);
}
And repository:和存储库:
@Repository
public interface ArticleRepository extends MongoRepository<Article, String> {
}
So, now I'm trying to create a method that will get me one random record from my collection Article
also, I want to create a controller so when I go to some endpoint and submit some get method to retrieve one record from the collection and so I can check it in postman or with Swagger.因此,现在我正在尝试创建一种方法,该方法还将从我的收藏
Article
中获取一条随机记录,我想创建一个 controller 所以当我将 go 到某个端点并提交一些获取方法以从集合中检索一条记录时所以我可以在 postman 或 Swagger 中检查它。 I find some answers to similar question to mine but no one solved my problem, I want to have API for something like that.我找到了一些与我的类似问题的答案,但没有人解决我的问题,我想要 API 来解决类似的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.