Database Reference
In-Depth Information
{
"_id"
:
"First Post"
,
"author"
:
"Rick"
,
"text"
:
"This is my first post"
,
"comments"
:
[
{
"author"
:
"Stuart"
,
"text"
:
"Nice post!"
},
...
]
}
Although this schema works well for creating and displaying comments and posts, suppose
we wanted to add a feature that allows you to search for all the comments by a particular user.
The query (using this embedded schema) would be the following:
db
.
posts
.
find
(
{
'comments.author'
:
'Stuart'
},
{
'comments'
:
1
})
The result of this query, then, would be documents of the following form:
{
"_id"
:
"First Post"
,
"comments"
:
[
{
"author"
:
"Stuart"
,
"text"
:
"Nice post!"
},
{
"author"
:
"Mark"
,
"text"
:
"Dislike!"
} ] },
{
"_id"
:
"Second Post"
,
"comments"
:
[
{
"author"
:
"Danielle"
,
"text"
:
"I am intrigued"
},
{
"author"
:
"Stuart"
,
"text"
:
"I would like to subscribe"
} ] }
The major drawback to this approach is that we get back
much
more data than we actually
need.Inparticular,wecan'taskforjustStuart'scomments;wehavetoaskforpoststhatStuart
has commented on, which includes all the other comments on those posts as well. Further fil-
tering would then be required in our Python code:
def
def
get_comments_by
(
author
):
for
for
post
iin
db
.
posts
.
find
(
{
'comments.author'
:
author
},
{
'comments'
:
1
}):
for
for
comment
iin
post
[
'comments'
]:
iif
comment
[
'author'
]
==
author
:
yield
yield
post
[
'_id'
],
comment