ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

java-嵌套集合字段的Spring Mongodb分页

2019-10-27 03:02:14  阅读:259  来源: 互联网

标签:mongodb spring-data-mongodb pagination spring java


我在另一个文档中有一个文档集合.希望在获取数据时对嵌套元素执行分页.你能让我知道怎么做吗?在结构中,我想使用分页获取消息.

public abstract class CommonDomainAttributes implements Serializable, Cloneable {
    private static final long serialVersionUID = 1L;

    @Id
    protected String id;

    @JsonIgnore
    @CreatedDate
    protected Date createDate;

    //@JsonIgnore
    @LastModifiedDate
    //@JsonSerialize(using=JsonDateSerializer.class)
    protected Date lastModifiedDate;

    @JsonIgnore
    @CreatedBy
    protected String createdBy;

    @JsonIgnore
    @LastModifiedBy
    protected String lastModifiedBy;
    /**
     * @return the id
     */
    public String getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }
    /**
     * @return the createDate
     */
    public Date getCreateDate() {
        return createDate;
    }
    /**
     * @param createDate the createDate to set
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    /**
     * @return the lastModifiedDate
     */
    public Date getLastModifiedDate() {
        return lastModifiedDate;
    }
    /**
     * @param lastModifiedDate the lastModifiedDate to set
     */
    public void setLastModifiedDate(Date lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
    /**
     * @return the createdBy
     */
    public String getCreatedBy() {
        return createdBy;
    }
    /**
     * @param createdBy the createdBy to set
     */
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
    /**
     * @return the lastModifiedBy
     */
    public String getLastModifiedBy() {
        return lastModifiedBy;
    }
    /**
     * @param lastModifiedBy the lastModifiedBy to set
     */
    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (id == null ? 0 : id.hashCode());
        return result;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        CommonDomainAttributes other = (CommonDomainAttributes) obj;
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        return true;
    }




    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("CommonDomainAttributes [id=").append(id)
        .append(", createDate=").append(createDate)
        .append(", lastModifiedDate=").append(lastModifiedDate)
        .append(", createdBy=").append(createdBy)
        .append(", lastModifiedBy=").append(lastModifiedBy)
        .append(", toString()=").append(super.toString()).append("]");
        return builder.toString();
    }


}

public class Message extends CommonDomainAttributes implements Serializable{

    private String fromuserId;
    private String fromuserName;
    private String toUserId;
    private String touserName;
    private String message;
    /**
     * @return the fromuserId
     */
    public String getFromuserId() {
        return fromuserId;
    }
    /**
     * @param fromuserId the fromuserId to set
     */
    public void setFromuserId(String fromuserId) {
        this.fromuserId = fromuserId;
    }
    /**
     * @return the fromuserName
     */
    public String getFromuserName() {
        return fromuserName;
    }
    /**
     * @param fromuserName the fromuserName to set
     */
    public void setFromuserName(String fromuserName) {
        this.fromuserName = fromuserName;
    }
    /**
     * @return the toUserId
     */
    public String getToUserId() {
        return toUserId;
    }
    /**
     * @param toUserId the toUserId to set
     */
    public void setToUserId(String toUserId) {
        this.toUserId = toUserId;
    }
    /**
     * @return the touserName
     */
    public String getTouserName() {
        return touserName;
    }
    /**
     * @param touserName the touserName to set
     */
    public void setTouserName(String touserName) {
        this.touserName = touserName;
    }
    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }
    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Message [fromuserId=");
        builder.append(fromuserId);
        builder.append(", fromuserName=");
        builder.append(fromuserName);
        builder.append(", toUserId=");
        builder.append(toUserId);
        builder.append(", touserName=");
        builder.append(touserName);
        builder.append(", message=");
        builder.append(message);
        builder.append(", toString()=");
        builder.append(super.toString());
        builder.append("]");
        return builder.toString();
    }
}

@Document(collection="discussion")
@TypeAlias("discussion")
public class Discussion extends CommonDomainAttributes implements Serializable{

    private String discussionTopic;
    private List<Message> messages;
    /**
     * @return the discussionTopic
     */
    public String getDiscussionTopic() {
        return discussionTopic;
    }
    /**
     * @param discussionTopic the discussionTopic to set
     */
    public void setDiscussionTopic(String discussionTopic) {
        this.discussionTopic = discussionTopic;
    }
    /**
     * @return the messages
     */
    public List<Message> getMessages() {
        return messages;
    }
    /**
     * @param messages the messages to set
     */
    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }


    /**
     * @param messages the messages to set
     */
    public void addMessages(Message message) {
        if(null == messages){
            messages = new LinkedList<>();
        }
        messages.add(message);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Discussion [discussionTopic=");
        builder.append(discussionTopic);
        builder.append(", messages=");
        builder.append(messages);
        builder.append(", toString()=");
        builder.append(super.toString());
        builder.append("]");
        return builder.toString();
    }

}

解决方法:

一点关于Mongo查询语言

在MongoDB中,$slice运算符控制查询返回的数组的项数. $slice运算符可以接受以下语法的值:

[toSkip, toLimit]

第一个值指示要跳过的数组中的项目数,第二个值指示要返回的项目数.例如,您可以使用以下查询:

db.discussions.find({}, {messages: {$slice: [20, 10]}})

在跳过该数组的前20条消息后返回10条消息.

将其带入Spring Data World

为了将$slice运算符与Spring Data MongoDB一起使用,您应该使用@Query注释及其字段属性.例如,如果您有一个DiscussionRepository,则可以编写如下内容:

public interface DiscussionRepository extends MongoRepository<Discussion, String> {
    @Query(value = "{}", fields = "{messages: {$slice: [?0, ?1]}}")
    List<Discussion> findDiscussions(int skip, int limit);
}

通过这种安排,以下方法调用:

discussionRepository.findDiscussions(20, 10)

将产生与以下结果相同的结果:

db.discussions.find({}, {messages: {$slice: [20, 10]}})

只需一点工作,就可以将“跳过/限制”组合变为分页功能.

标签:mongodb,spring-data-mongodb,pagination,spring,java
来源: https://codeday.me/bug/20191027/1941382.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有