SpringBoot如何从MongoDB的json字符串字段查询?
pom引入
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
实体类
@Datapublic class Person {
private String id;
private String name;
private int age;
private String data;
}
查询类
@Servicepublic class MongoService {
@Resource
private MongoTemplate mongoTemplate;
public Person findPerson()
{
// Person person = new Person();
// person.setName("ace");
// person.setAge(23);
// person.setData("{\"accounts\":{\"balance\":100,\"ext\":\"hi1\"}}");
// mongoTemplate.insert(person);
BasicQuery q1 = new BasicQuery("{ age : { $lt : 30 }}");
System.out.println(mongoTemplate.find(q1, Person.class));
BasicQuery q2 = new BasicQuery("{ data.accounts.balance : { $gt : 500 }}");
System.out.println(mongoTemplate.find(q2, Person.class));
return mongoTemplate.findOne(new Query(where("name").is("ace")), Person.class);
}
}
数据
好像data字段的数据格式不对,该怎么存入呢?
文档地址
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.query
报错
以上是 SpringBoot如何从MongoDB的json字符串字段查询? 的全部内容, 来源链接: utcz.com/p/944186.html