如何在Grails中对复杂的mongoDB查询进行分页?

时间:2012-12-11 09:26:44

标签: mongodb grails gorm gorm-mongodb

我有三个域类作者,评论和书籍:

class Author {
String name
static hasMany = [books: Book, comments: Comment]
}

class Book {
static belongsTo = [author: Author]
static hasMany = [comments: Comment]
}

class Comment {
static belongsTo = [book: Book, author: Author] 
}

我有以下mongoDB查询来查找所有具有给定作者评论的书籍。

Comment.findAllByAuthor(author1).collect {
        it.book
    }.unique().findAll { 
        it != null 
}

如何为此查询使用分页,即对所有书籍进行分页?

1 个答案:

答案 0 :(得分:1)

使用Book查询而不是Comment:

def c = Book.createCriteria()
def PaginatedBookList = c.list(max: params.max, offset: params.offset) {
    and{
        eq('author',author1)
        isNotNull('comments')
    }
}