1

Member.class

@Document(collection = "alfagift_member")
public class Member {

    @Field(value = "full_name")
    private String fullName;

    @Field(value = "member_id")
    private String memberId;

    @Field(value = "pin")
    private Pin pin;
}

Pin.class

public class Pin {

    @Field(value = "encrypted_value")
    private String encryptedValue;
    
    @Field(value = "last_blocked")
    private Date lastBlocked;
}

MemberRepository.class

@Repository
public interface MemberRepository extends MongoRepository<Member, String> {

  @Query(value="{'member_id' : ?0}, {'pin.last_blocked' : ?1}")
  Member findByMemberIdAndLastBlockedGreaterThan(Integer memberId, Date now);
}

Then I tried to make a query method like this, but the function of greaterthan itself doesn't work

I was confused, how to solve it ??

** EDITT

Data in MongoDB

{
  "_id": ObjectId("5f1fa9876b911d2b27d0bcf5"),
  "full_name": "this is my name",
  "pin": {
    "encrypted_value": "e9b0d0aae93291679b304f9d3c058029fa4253ccaf366cae3aeb473099b7aff8:qcfq9josgstqjcoun0isnvmja3",
    "last_blocked": ISODate("2020-08-03T05:00:31.597Z")
  },
  "member_id": 2200146,
  "_class": "id.alfadigital.alfagift.service.account.v1.db.mongo.domain.Member"
}

And parameter sending

Date now = new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1));
Member member = memberRepository.findByMemberIdAndLastBlockedGreaterThan(memberId, now);    
7
  • Have you tried this @Query(" {'member_id : ?0 '} ,{'pin.last_blocked': { $gt : ?1}}") Commented Aug 3, 2020 at 5:41
  • I have tried but the results are still the same @VishalPawar Commented Aug 3, 2020 at 6:04
  • can you what are you sending date parameter and what database has? Commented Aug 3, 2020 at 6:05
  • Have you tried simply removing the @Query and using AndPinLastBlockedGreaterThan? Commented Aug 3, 2020 at 6:10
  • I have edited the document, you can see **EDITT @VishalPawar Commented Aug 3, 2020 at 6:12

1 Answer 1

2
  • Update the repository like this
    @Repository
    public interface MemberRepository 
                             extends MongoRepository<Member, String> {

   
        Member findByMemberIdAndPinLastBlockedGreaterThan(Integer memId, 
                                                          Date now);
    }
  • You are deducting one hour from current time and your stored document has blocked time before that. For testing purpose, reduct 5 hours and query.
   Date now = new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(5));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.