如何在Spring Data Mongo DB中一起在何处和何处使用条件?

我有一个名为Employee的文档。我想查询所有在2019年至2020年之间指定为“开发人员”并加入日期的员工。

class Employee{
    private String name;
    private String designation;
    private Date joiningDate;
}

**// i want where condition on designation to be included in the following** 
@Repository
public interface EmployeeRepository extends MongoRepository<Employee,String> {
    @Query("{'joiningDate' : {$gte : ?0,$lte : ?1}}")
    public List<Employee> findByJoiningDateBetween(LocalDateTime startTime,LocalDateTime endTime);
}
feng199032 回答:如何在Spring Data Mongo DB中一起在何处和何处使用条件?

真的,这不是JPA,这是Spring Data MongoDB。

这不是文档,文档必须注释为@Document并具有@Id(字符串或更好的ObjectId)。

如果要使用复杂的查询,也许是使用自己的存储库方法实现的更好方法:

public interface EmployeeRepository extends MongoRepository<Employee,String>,EmployeeRepositoryBase {
}

public interface EmployeeRepositoryBase {
    List<Employee> findByJoiningDateBetween(Date startDate,Date endDate);
}

@Repository
public class EmployeeRepositoryImpl implements EmployeeRepositoryBase {

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<Employee> findJoiningDateBetween(final Date startDate,final Date endDate) {
        final var query = Query.query(Criteria.where("...").is(...).and("...").gte(...).lte(...);
        return mongoTemplate(query,Employee.class);
    }
}
,

在您的存储库中使用这种方式

List<Employee> findByJoiningDateBetween(Date startDate,Date endDate);

refer

本文链接:https://www.f2er.com/3056058.html

大家都在问