使用子字符串过滤 GAE 关键字段
Filter GAE Key Field with Substring
我有一个名为 Posts 的类型,键是 postId。
postId 格式为:YYMMDDXXX。其中 XXX 是 3 位序列号。
例如:150703001、150704001、150704002
如何从数据存储中的实体获取序列号?我想将此 SQL Select nvl(max(substring(postId, 7, 3)), 0) from posts where substring(postId, 1, 6) = '150704'
转换为 Objectivy 过滤器。
请帮忙,非常感谢!
您必须将 Kinds/Entities 重新设计成不同的结构才能完成此操作。数据存储无法按子字符串搜索。
一种可能的方法是这种布局:
@Entity
class PostGroup {
@Id
String id; // this is will hold the formatted date YYMMDD
}
@Entity
class Post {
@Id
Long id; // auto generated ID
@Index
Long sequence; // the sequence number
@Parent
Key<PostGroup> group;
}
现在您可以为您的 Objectify 查询设置 PostGroup
具有 祖先 ,sort by Post.sequence desc limit 1
以查找最新的 post。
String yymmdd = "150704";
Key<PostGroup> postGroupKey = Key.create(PostGroup.class, yymmdd);
Query<Post> query = ofy().type(Post.class).ancestor(postGroupKey).order("-sequence");
Post latestPost = query.first().now();
注意:您不必实际保留 PostGroup
即可将它们用作祖先。
我有一个名为 Posts 的类型,键是 postId。
postId 格式为:YYMMDDXXX。其中 XXX 是 3 位序列号。
例如:150703001、150704001、150704002
如何从数据存储中的实体获取序列号?我想将此 SQL Select nvl(max(substring(postId, 7, 3)), 0) from posts where substring(postId, 1, 6) = '150704'
转换为 Objectivy 过滤器。
请帮忙,非常感谢!
您必须将 Kinds/Entities 重新设计成不同的结构才能完成此操作。数据存储无法按子字符串搜索。
一种可能的方法是这种布局:
@Entity
class PostGroup {
@Id
String id; // this is will hold the formatted date YYMMDD
}
@Entity
class Post {
@Id
Long id; // auto generated ID
@Index
Long sequence; // the sequence number
@Parent
Key<PostGroup> group;
}
现在您可以为您的 Objectify 查询设置 PostGroup
具有 祖先 ,sort by Post.sequence desc limit 1
以查找最新的 post。
String yymmdd = "150704";
Key<PostGroup> postGroupKey = Key.create(PostGroup.class, yymmdd);
Query<Post> query = ofy().type(Post.class).ancestor(postGroupKey).order("-sequence");
Post latestPost = query.first().now();
注意:您不必实际保留 PostGroup
即可将它们用作祖先。