英文:
How to query with greaterThan for an embedded document using Spring Data MongoDB Repository?
问题
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' : { $gt : ?1 } }")
Member findByMemberIdAndLastBlockedGreaterThan(Integer memberId, Date now);
}
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);
英文:
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);
答案1
得分: 2
- 更新存储库如下:
@Repository
public interface MemberRepository
extends MongoRepository<Member, String> {
Member findByMemberIdAndPinLastBlockedGreaterThan(Integer memId,
Date now);
}
- 您正在从当前时间减去一小时,而您存储的文档在此之前已被阻止。为了测试目的,减少5小时并查询。
Date now = new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(5));
英文:
- 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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论