Database Reference
In-Depth Information
current
=
current
.
replies
[
part
]
comment
=
current
Approach: Hybrid Schema Design
In the “hybrid approach,” we store comments in “buckets” that hold about 100 comments.
Consider the following example document:
{
_id
:
ObjectId
(...),
node_id
:
ObjectId
(...),
page
:
1
,
count
:
42
,
comments
:
[ {
slug
:
'34db'
,
posted
:
ISODateTime
(...),
author
:
{
id
:
ObjectId
(...),
name
:
'Rick'
},
text
:
'This is so bogus ... '
},
... ]
}
Each document maintains
page
and
count
data that contains metadata regarding the page
number and the comment count in this page, in addition to the
comments
array that holds the
comments themselves.
Operation: Post a new comment
In order to post a new comment, we need to
$push
the comment onto the last page and
$inc
that page's comment count. Consider the following example that adds a comment onto the last
page of comments for some
node
:
def
def
post_comment
(
node
,
comment
):
result
=
db
.
comment_pages
.
update
(
{
'node_id'
:
node
[
'_id'
],
'page'
:
node
[
'num_comment_pages'
],
'count'
: {
'$lt'
:
100
} },
{
'$inc'
: {
'count'
:
1
},
'$push'
: {
'comments'
:
comment
} },
upsert
=
True
)
iif
not
not
result
[
'updatedExisting'
]:
db
.
cms
.
nodes
.
update
(