Database Reference
In-Depth Information
node
=
db
.
cms
.
nodes
.
find_one
(
{
...
node specification
...
},
{
...
some fields relevant to your page
from
...
,
'metadata.comments'
: {
'$slice'
: [
page_num
*
page_size
,
page_size
] }
from
the
the root
root discussion
discussion ...
})
To return paginated comments for the threaded design, we must retrieve the whole document
and paginate the comments within the application:
node
=
db
.
cms
.
nodes
.
find_one
(
...
node specification
...
)
def
def
iter_comments
(
obj
):
for
for
reply
iin
obj
[
'replies'
]:
yield
yield
reply
for
for
subreply
iin
iter_comments
(
reply
):
yield
yield
subreply
paginated_comments
=
itertools
.
slice
(
iter_comments
(
node
),
page_size
*
page_num
,
page_size
*
(
page_num
+
1
))
Operation: Retrieve a comment via direct links
Instead of retrieving comments via slugs as in
Approach: One Document per Comment
, the
following example retrieves comments using their position in the comment list or tree. For
chronological (i.e., nonthreaded) comments, we'll just use the
$slice
operator to extract a
single comment, as follows:
node
=
db
.
cms
.
nodes
.
find_one
(
{
'node_id'
:
node_id
},
{
'comments'
: {
'$slice'
: [
position
,
position
] } })
comment
=
node
[
'comments'
][
0
]
For threaded comments, we must know the correct path through the tree in our application, as
follows:
node
=
db
.
cms
.
nodes
.
find_one
(
...
node specification
...
)
current
=
node
.
metadata
for
for
part
iin
path
: