如何使用Spring Data MongoDB Repository查询嵌套文档的大于条件?

huangapple go评论99阅读模式
英文:

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 = &quot;alfagift_member&quot;)
public class Member {

    @Field(value = &quot;full_name&quot;)
    private String fullName;

    @Field(value = &quot;member_id&quot;)
    private String memberId;

    @Field(value = &quot;pin&quot;)
    private Pin pin;
}

Pin.class

public class Pin {

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

MemberRepository.class

@Repository
public interface MemberRepository extends MongoRepository&lt;Member, String&gt; {

  @Query(value=&quot;{&#39;member_id&#39; : ?0}, {&#39;pin.last_blocked&#39; : ?1}&quot;)
  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

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

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&lt;Member, String&gt; {

   
        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));

huangapple
  • 本文由 发表于 2020年8月3日 13:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63224208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定