2013-01-06 23 views
0

我与Morphia有一些问题。 有人可以帮我吗?Morphia。如何从大数据存储获取信息的一部分

我在Spring + MongoDB上撰写关于电影和名人的网络项目。 我有实体类类型:

@Entity(value="genres") 
public class Genre implements IGenre { 

    @Id 
    @Indexed 
    private ObjectId id; 

    @Indexed 
    private String name; 

    private String description; 

    private long quantity; 

    private Set <IMovie> movies; 

    //getters and setters 

} 

而实体类的电影:

@Entity(value="movies") 
public class Movie implements IMovie { 

    @Id 
    @Indexed 
    private ObjectId id; 

    @Indexed 
    private String originalTitle; 

    private String year; 

    private Set <IGenre> genres; 

    // getters and setters 

} 

我有30个流派。例如其中之一:喜剧。 另外我有25万喜剧。 现在我想按照流派=喜剧来做电影分页。 我如何才能从所有喜剧中获得20条记录。 如果我使用@Embedded或@Reference注解,我仍然会一次获得整个列表。这对于在控制器中使用它很重要。

回答

0

你应该改变你的数据模式来做这样的查询。您使用的架构具有循环依赖性,在您持有电影实体的流派实体中以及持有流派的电影中。根据流派持有所有电影也不容易查询。如果我是你,我会使用这样的模式。

@Entity(noClassnameStored = true) // you wouldn't have any problem when you change the class name or package of your class if you don't store the classname through this annotation 
    public class Movie implements IMovie { 

     @Id 
     @Indexed 
     private ObjectId id; 

     @Indexed 
     private String originalTitle; 

     private String year; 

     private Set <String> genres; // unique identifier of the genres instead of embedding the whole genre entity 

     // getters and setters 

    } 

因此,拥有这样的模式,您可以通过在流派字段中写入简单的$ in查询来检索具有特定流派的电影。针对您的案例的示例查询:

datastore.find(Movie.class).field("genres").in(Lists.newArrayList("comedy")).limit(20).asList; 

在下面的mongo网页中,您可以找到关于如何根据不同场景设计架构的建议。

http://docs.mongodb.org/manual/core/data-modeling/#data-modeling-patterns-and-examples

+0

非常感谢你。 – user1952756

+0

不客气 – cubbuk

0

我对Mongo不太熟悉,但看起来您需要在此处实现自定义查询。无论你做什么,你都需要通过一个开始页面,以及一个页面大小(在你的情况下20)。

您可以通过在查询上结合.offset(page_start)和.limit(page_size)在Morphia中进行分页。因此,首先您将创建一个查询来获取属于某种类型的电影,然后应用分页。

貌似是用它办理了仓库春http://static.springsource.org/spring-data/data-mongodb/docs/1.0.0.RELEASE/reference/html/#repositories.special-parameters

你会使用可分页实现通过传呼数据,而不用担心做偏移和限制要求自己。文档中还有一个“Web分页”的例子。

希望这会有所帮助!