Database Reference
In-Depth Information
{
'comments'
:
1
})
for
for
comment
iin
page
[
'comments'
]:
iif
comment
[
'slug'
]
=
comment_slug
:
break
break
To perform this query efficiently, we'll need a new index on
node_id, comments.slug
(this
is assuming that slugs are only guaranteed unique
within
a node):
>>>
>>>
db
.
comment_pages
.
ensure_index
([
...
...
(
'node_id'
,
1
), (
'comments.slug'
,
1
)])
Sharding Concerns
For all of the architectures just discussed, we will want the
node_id
field to participate in any
shard key we pick.
For applications that use the “one document per comment” approach, we'll use the
slug
(or
full_slug
, in the case of threaded comments) fields in the shard key to allow the
mongos
in-
stances to route requests by
slug
:
>>>
>>>
db
.
command
(
'shardcollection'
,
'dbname.comments'
, {
...
...
'key'
: {
'node_id'
:
1
,
'slug'
:
1
} })
{ "collectionsharded" : "dbname.comments", "ok" : 1 }
In the case of comments that are fully embedded in parent content, the comments will just
participate in the sharding of their parent document.
For hybrid documents, we can use the page number of the comment page in the shard key
alongwiththe
node_id
topreventasinglediscussionfromcreatingagiant,unsplittablechunk
of comments. The appropriate command for this is as follows:
>>>
>>>
db
.
command
(
'shardcollection'
,
'dbname.comment_pages'
, {
...
...
key
: {
'node_id'
:
1
,
'page'
:
1
} })
{ "collectionsharded" : "dbname.comment_pages", "ok" : 1 }