Mongodb 使用条件查询搜索Mongo Respositions,MongoTemplate在Mongo+中无法正常工作;springBoot应用程序
在这个应用程序中,我还插入了一个功能,允许我在数据库存储库的对象中查找特定项目。为此,我使用了mongo模板。 我的存储库中的每个对象都包含以下项:Mongodb 使用条件查询搜索Mongo Respositions,MongoTemplate在Mongo+中无法正常工作;springBoot应用程序,mongodb,spring-boot,aggregate,criteria,mongotemplate,Mongodb,Spring Boot,Aggregate,Criteria,Mongotemplate,在这个应用程序中,我还插入了一个功能,允许我在数据库存储库的对象中查找特定项目。为此,我使用了mongo模板。 我的存储库中的每个对象都包含以下项: [ { "id": "5f759b198dfb247ccd6280b2", "name": "Probando lo que cree", "text": "Enrique Gordo
[
{
"id": "5f759b198dfb247ccd6280b2",
"name": "Probando lo que cree",
"text": "Enrique Gordon",
"description": "Un poquito de todo ",
"images": [
"R0lGODlhLAEsAff"
],
"videos": [
"AAAAIGZ0eXBpc29"
],
"date": null,
"allComments": null
},
{
"id": "5f759d2b8dfb247ccd6280ba",
"name": "Probando lo que cree",
"text": "Enrique Gordon",
"description": "Algo nuevo",
"images": [
"R0lGODlhLAEsAff"
],
"videos": [
"AAAAIGZ0eXBpc29"
],
"date": null,
"allComments": null
},
{
"id": "5f75a5e2275d7d34914d2d98",
"name": "Zamorano",
"text": "Zamorano",
"description": "Zamorano",
"images": [
"iVBORw0KGgoAAAA",
"/9j/4T/+RXhpZgA"
],
"videos": [
"AAAAIGZ0eXBpc29"
],
"date": null,
"allComments": null
}
]
因此,考虑到这一点,我在存储库中初始化了一个函数,该函数返回一个包含以下项的查询:描述、文本和名称
REPOSITORY
public List<Post> searchPosts(String search){
return mongoTemplate.aggregate(Aggregation.newAggregation(
Aggregation.match(new Criteria().orOperator(
Criteria.where("text").regex(search),
Criteria.where("description").regex(search),
Criteria.where("name").regex(search),
))
),"Post",Post.class).getMappedResults();
}
*The post is the class already initilized with getters and setters, having in mind the concepts text, name , and description too
存储库
公共列表搜索帖子(字符串搜索){
返回mongoTemplate.aggregate(Aggregation.newAggregation(
Aggregation.match(新条件().orOperator(
标准。其中(“文本”)。正则表达式(搜索),
标准。其中(“说明”)。正则表达式(搜索),
标准。其中(“名称”)。正则表达式(搜索),
))
),“Post”,Post.class).getMappedResults();
}
*post是已经使用getter和setter初始化的类,同时还要考虑文本、名称和描述等概念
然后在我的终点,记住这些标准,我把它带来的任何东西作为路径变量传递到我的终点
CONTROLLER
@GetMapping("/post/{search}/search")
public List<Post> getSearchedPosts(@PathVariable ("search") String search){
return postRepository.searchPosts(search);
}
控制器
@GetMapping(“/post/{search}/search”)
公共列表getSearchedPosts(@PathVariable(“搜索”)字符串搜索){
返回postRepository.searchPosts(搜索);
}
但不管出于什么原因,当我在《邮递员》中测试这个过程时,它会给我带来所有的对象,但不会带来被查询的对象
我是否省略了java查询函数中的某些内容?。
提前感谢您不需要为此进行聚合 试试这个-希望它能起作用
Query query= new Query(
new Criteria()
.orOperator(
Criteria.where("text").regex(search),
Criteria.where("description").regex(search),
Criteria.where("name").regex(search)
)
);
return mongoTemplate.find(query, Post.class);
我已经用您提供的示例文档在我的本地env上尝试了这一点。我使用Zamorano
作为搜索字符串。我只得到一个结果。这是我的密码-
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@AllArgsConstructor
public class SOController {
@Autowired
MongoTemplate mongoTemplate;
@GetMapping("/post/{search}/search")
public List<Post> getSearchedPosts(@PathVariable("search") String search) {
Query query = new Query(
new Criteria()
.orOperator(
Criteria.where("text").regex(search),
Criteria.where("description").regex(search),
Criteria.where("name").regex(search)
)
);
return mongoTemplate.find(query, Post.class, "so");
}
}
导入lombok.allargsconstuctor;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.data.mongodb.core.MongoTemplate;
导入org.springframework.data.mongodb.core.query.Criteria;
导入org.springframework.data.mongodb.core.query.query;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RestController;
导入java.util.List;
@RestController
@AllArgsConstructor
公共类控制器{
@自动连线
MongoTemplate MongoTemplate;
@GetMapping(“/post/{search}/search”)
公共列表getSearchedPosts(@PathVariable(“搜索”)字符串搜索){
查询=新查询(
新准则()
奥勒操作员(
标准。其中(“文本”)。正则表达式(搜索),
标准。其中(“说明”)。正则表达式(搜索),
标准.其中(“名称”).regex(搜索)
)
);
返回mongoTemplate.find(查询,Post.class,“so”);
}
}
这是邮递员的img-
PS-确保您的导入是正确的。感谢您对wak786的支持,但仍保持相同的行为方式。。基于您发布的文档。你能提供一些我可以尝试使用的示例搜索字符串吗?我在beggining网站上发布的对象数组非常有用,在那里,所有暴露的数据都与存储库和控制器类相处得很好。我想了解在这个方法中传递的
搜索字符串的示例<代码>postRepository.searchPosts(搜索)