gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:12:11 25 4
gpt4 key购买 nike

我在另一个文档中有一个文档集合。想在获取数据时对嵌套元素实现分页。你能告诉我该怎么做吗?在结构中,我想使用分页获取消息。

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


为了在 Spring Data MongoDB 中使用 $slice 运算符,您应该使用 @Query注释及其 fields 属性。例如,如果你有一个 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]}})

通过一些工作,您可以将Skip/Limit 组合变成分页功能。

关于java - 嵌套集合字段的Spring Mongodb分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com