Spring Boot中的Mongo查询

时间:2018-09-05 15:32:40

标签: spring mongodb

请帮助我在春季使用mongoTemplate获取以下复杂的mongo查询

仅当“ NAME”:“ UserID1”时,我才想检索集合吗?

True foo 1 <QuerySet []>
Save Called!
Fire of like saved signal
Fire of like saved signal

谢谢。

1 个答案:

答案 0 :(得分:0)

例如,假设您的班级是这样的:

@Document
public class FirstClass{
  @Id
  String id;
  List<SecondClass> A;

  public class SecondClass{
     string Name;
     List<ThirdClass> C1;

  }
}

在mongoTemplate中:

...
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
...

@Component
    public ServiceClass{
           private final MongoTemplate mongoTemplate;

           ServiceClass(MongoTemplate mongoTemplate){
                this.mongoTemplate = mongoTemplate;
           }

           public List<FirstClass> getResult(String searchInput){
                final Criteria criteria =
                    Criteria.where("A.name").is(searchInput);
                List<FirstClass> result = mongoTemplate.find(new Query(criteria),FirstClass.class);
           }
    }

您可以像这样使用spring数据mongo查询:

public interface FirstClassRepository extends MongoRepository<FirstClass, String> {
        List<FirstClass> findByA_Name(String name);
}