如何在Spring Data Mongo中查询生日

时间:2019-08-30 05:41:59

标签: mongodb spring-data-mongodb

我无法到达查询以获取所有生日为今天的记录。我只需要匹配日期和月份

我尝试了许多组合,包括$ match和$ expr。似乎什么都没用。

public interface GenericPolicyRepository extends MongoRepository<GenericPolicy, String> {
    @Query("{ 'agentId': ?0 , '$expr': { '$eq': [{ '$month': '$dateOfBirth' }, ?1]}, '$expr': { '$eq': [{ '$dayOfMonth': '$dateOfBirth' }, ?2]}}")
    public List<GenericPolicy> findAllBirthDaysByAgent(String agentId, String month, String day);
}

//通用策略类

@Document("Policy")
public class GenericPolicy {  
    @Id
    private String policyId;

    private String agentId;
    private String insuredUserId;
    private String status;
    private Date policyStartDate;
    private Date policyEndDate;
    private String paymentReferenceNumber;
    private BigInteger premium;
    private Date dateofSubmission;
    private String submissionStatus;
    private InsuredDetails insuredDetails;
}

//保险明细

 public class InsuredDetails {

        @NotNull
        private String nationality;
        @NotNull
        private String fullName;
        @NotNull
        private String gender;
        @NotNull
        private Date dateOfBirth;
        @NotNull
        private String identityType;
        @NotNull
        private String identityNumber;
        @NotNull
        @Email
        private String emailaddress;
        @NotNull
        @Size(max = 10,min = 10)
        private String mobileNumber;
        @NotNull
        private String addressLine1;
        @NotNull
        private String addressLine2;
        @NotNull
        private String postCode;
    }

2 个答案:

答案 0 :(得分:1)

在查询中尝试以下操作,其中dateOfBirth是保存在您的收藏夹中的键,此处我们将月份中的当前日期和月份与存储在收藏夹中的一个相匹配

  {
            $expr: {
                        "$and": [
                            {"$eq": [{"$dayOfMonth": "$dateOfBirth"}, {"$dayOfMonth": new Date()}]},
                            {"$eq": [{"$month": "$dateOfBirth"}, {"$month": new Date()}]}
                        ]
                    }
  }

答案 1 :(得分:0)

基于上述类,给定文档具有嵌套文档 insuredDetails (假定在db中也具有相同的名称)。该字段具有 dateOfBirth 字段。牢记这一事实,查询可以编写如下。

public interface GenericPolicyRepository extends MongoRepository<GenericPolicy, String> {
    @Query("{'agentId': ?0 , '$expr': {'$and': [{'$eq': [{'$dayOfMonth': '$insuredDetails.dateOfBirth'}, {'$dayOfMonth': ?1}]}, {'$eq': [{'$month': '$insuredDetails.dateOfBirth'}, {'month': ?1}]}]}}")
    public List<GenericPolicy> findAllBirthDaysByAgent(String agentId, Date birthDate);
}

用法

public class GenericPolicyServiceImpl implements GenericPolicyService {

    @Autowired
    GenericPolicyRepository genericPolicyRepository;

    public List<GenericPolicy> getAllBirthDaysByAgent(String agentId, String month, String day){
        Calendar c = Calendar.getInstance();
        int month = Integer.parseInt(month);
        int day = Integer.parseInt(day);
        c.set(0, month, day, 0, 0);
        Date birthDate = c.getTime();
        genericPolicyRepository.findAllBirthDaysByAgent(agentId, birthDate);
    }
}