Database Reference
In-Depth Information
The other dependent collection we'll use is
social.news
, posts from people the user follows.
This schema includes much of the same information as the
social.wall
information, so this
document has been abbreviated for clarity:
{
_id
:
ObjectId
(...),
user_id
:
"T4Y...AE"
,
month
:
'201204'
,
posts
:
[ ... ]
}
Operations
Since these schemas optimize for read performance at the possible expense of write perform-
ance, a production system should provide a queueing system for processing updates that may
take longer than the desired web request latency.
Viewing a News Feed or Wall Posts
The most common operation on a social network is probably the display of a particular user's
news feed, followed by a user's wall posts. Because the
social.news
and
social.wall
col-
lections are optimized for these operations, the query is fairly straightforward. Since these two
collections share a schema, viewing the posts for a news feed or a wall are actually quite sim-
ilar operations, and can be supported by the same code:
def
def
get_posts
(
collection
,
user_id
,
month
=
None
):
spec
=
{
'user_id'
:
viewed_user_id
}
iif
month
iis not
not
None
:
spec
[
'month'
]
=
{
'$lte'
:
month
}
cur
=
collection
.
find
(
spec
)
cur
=
cur
.
sort
(
'month'
,
-
1
)
for
for
page
iin
cur
:
for
for
post
iin
reversed
(
page
[
'posts'
]):
yield
yield
page
[
'month'
],
post
The function
get_posts
will retrieve all the posts on a particular user's wall or news feed
in reverse-chronological order. Some special handling is required to efficiently achieve the
reverse-chronological ordering: