Database Reference
In-Depth Information
Here, the
replies
field in each comment holds the subcomments, which can in turn hold sub-
comments.
Operation: Post a new comment
To post a new comment in a chronologically ordered (i.e., unthreaded) system, we need the
following
update
:
db
.
cms
.
nodes
.
update
(
{
...
node specification
...
},
{
'$push'
: {
'metadata.comments'
: {
'posted'
:
datetime
.
utcnow
(),
'author'
:
author_info
,
'text'
:
comment_text
} } } )
The
$push
operator inserts comments into the
comments
array in correct chronological order.
For threaded discussions, the update operation is more complex. To reply to a comment, the
following code assumes that it can retrieve the
path
as a list of positions, for the parent com-
ment:
iif
path
!=
[]:
str_path
=
'.'
.
join
(
'replies.
%d
'
%
part
for
for
part
iin
path
)
str_path
+=
'.replies'
else
else
:
str_path
=
'replies'
db
.
cms
.
nodes
.
update
(
{
...
node specification
...
},
{
'$push'
: {
'metadata.'
+
str_path
: {
'posted'
:
datetime
.
utcnow
(),
'author'
:
author_info
,
'text'
:
comment_text
} } } )
This constructs a field name of the form
metadata.replies.0.replies.2...
as
str_path
and then uses this value with the
$push
operator to insert the new comment into the
replies
array.
Operation: View paginated comments
To view the comments in a nonthreaded design, we need to use the
$slice
operator: