Spring数据MongoDB字段字段的索引声明
Spring Data MongoDB index declaration for field of field
我有以下Spring数据MongoDB文档:
@Document(collection = "messages")
@CompoundIndexes({ @CompoundIndex(name = "chatId_messageId", def = "{'chatId': 1, 'messageId': 1}") })
public class Message implements Serializable {
private static final long serialVersionUID = -1590112217771926691L;
@Id
private String id;
private Long chatId;
private Integer messageId;
private Post post;
}
Post
模型(不是Spring数据MongoDB文档)看起来像:
public class Post implements Serializable {
private static final long serialVersionUID = -886664249197573502L;
private String id;
}
我想为 Message.post.id
字段添加索引。
如何在 Message
文档中使用 Spring 数据 MongoDB 和 Message.post
字段声明来完成?
如果您想将 Message.post.id
添加到一个已经存在的复合索引中,请像
这样操作
@Document(collection = "messages")
@CompoundIndexes({ @CompoundIndex(name = "chatId_messageId", def = "{'chatId': 1, 'messageId': 1, 'Message.post.id' : 1}") })
public class Message implements Serializable {
private static final long serialVersionUID = -1590112217771926691L;
@Id
private String id;
private Long chatId;
private Integer messageId;
private Post post;
}
复合索引是具有多个索引字段的索引,因此理想情况下,限制性最强的字段应该位于 B 树的左侧。例如,如果你想按性别和出生索引,索引应该从出生开始,因为它比性别更严格。
或者,如果您想将其视为单独的索引,则使用 @Indexed
创建索引,例如
public class Post implements Serializable {
private static final long serialVersionUID = -886664249197573502L;
@Indexed
private String id;
}
已更新
有关复合索引子字段的查询如何工作的更多信息,请查看Documentation
我有以下Spring数据MongoDB文档:
@Document(collection = "messages")
@CompoundIndexes({ @CompoundIndex(name = "chatId_messageId", def = "{'chatId': 1, 'messageId': 1}") })
public class Message implements Serializable {
private static final long serialVersionUID = -1590112217771926691L;
@Id
private String id;
private Long chatId;
private Integer messageId;
private Post post;
}
Post
模型(不是Spring数据MongoDB文档)看起来像:
public class Post implements Serializable {
private static final long serialVersionUID = -886664249197573502L;
private String id;
}
我想为 Message.post.id
字段添加索引。
如何在 Message
文档中使用 Spring 数据 MongoDB 和 Message.post
字段声明来完成?
如果您想将 Message.post.id
添加到一个已经存在的复合索引中,请像
@Document(collection = "messages")
@CompoundIndexes({ @CompoundIndex(name = "chatId_messageId", def = "{'chatId': 1, 'messageId': 1, 'Message.post.id' : 1}") })
public class Message implements Serializable {
private static final long serialVersionUID = -1590112217771926691L;
@Id
private String id;
private Long chatId;
private Integer messageId;
private Post post;
}
复合索引是具有多个索引字段的索引,因此理想情况下,限制性最强的字段应该位于 B 树的左侧。例如,如果你想按性别和出生索引,索引应该从出生开始,因为它比性别更严格。
或者,如果您想将其视为单独的索引,则使用 @Indexed
创建索引,例如
public class Post implements Serializable {
private static final long serialVersionUID = -886664249197573502L;
@Indexed
private String id;
}
已更新
有关复合索引子字段的查询如何工作的更多信息,请查看Documentation