Database Reference
In-Depth Information
new_skip = skip - page [ 'count' ]
iif new_skip >= 0 :
skip = new_skip
continue
continue
elif
elif skip > 0 :
comments = page [ 'comments' ][ skip :]
else
else :
comments = page [ 'comments' ]
skip = new_skip
for
for comment iin comments :
iif limit == 0 :
break
break
limit -= 1
yield
yield comment
iif limit == 0 : break
break
Here, we iterate through the pages until our skip requirement is satisfied, then yield com-
ments until our limit requirement is satisfied. For example, if we have three pages of com-
ments with 100, 102, 101, and 22 comments on each page, and we wish to retrieve comments
where skip=300 and limit=50 , we'd use the following algorithm:
Skip Limit Discussion
300 50
Page 0 has 100 comments, so skip -= 100 .
200 50
Page 1 has 102 comments, so skip -= 102 .
98 50
Page 2 has 101 comments, so set skip=0 and return last 3 comments.
0
47
Page 3 has 22 comments, so return them all and set limit-= 22 .
0
25
There are no more pages; terminate loop.
Operation: Retrieve a comment via direct links
To retrieve a comment directly without paging through all preceding pages of commentary,
we'll use the slug to find the correct page, and then use application logic to find the correct
comment:
page = db . comment_pages . find_one (
{ 'node_id' : node_id ,
'comments.slug' : comment_slug },
Search WWH ::




Custom Search