Database Reference
In-Depth Information
▪ A
posted
timestamp
▪ An
author
subdocument that contains a reference to a user's profile in the
id
field and
their name in the
name
field
▪ The full
text
of the comment
In order to support threaded comments, we need to use a slightly different structure:
{
_id
:
ObjectId
(...),
node_id
:
ObjectId
(...),
parent_id
:
ObjectId
(...),
slug
:
'34db/8bda'
full_slug
:
'2012.02.08.12.21.08:34db/2012.02.09.22.19.16:8bda'
,
posted
:
ISODateTime
(...),
author
:
{
id
:
ObjectId
(...),
name
:
'Rick'
},
text
:
'This is so bogus ... '
}
This structure:
▪ Adds a
parent_id
field that stores the contents of the
_id
field of the parent comment
▪ Modifies the
slug
field to hold a path composed of the parent or parent's slug and this
comment's unique slug
▪ Adds a
full_slug
field that combines the slugs and time information to make it easier to
sort documents in a threaded discussion by date
Operation: Post a new comment
To post a new comment in a chronologically ordered (i.e., without discussion threading) sys-
tem, we just need to use a regular
insert()
:
slug
=
generate_pseudorandom_slug
()
db
.
comments
.
insert
({
'node_id'
:
node_id
,
'slug'
:
slug
,