Database Reference
In-Depth Information
iif
not
not
circles_user_is_in
:
# user is not circled by poster; post is invisible
return
return
False
iif
post
[
'circles'
]
==
[
'*circles*'
]:
# post is public to all followed users; post is visible
return
return
True
for
for
circle
iin
post
[
'circles'
]:
iif
circle
iin
circles_user_is_in
:
# User is in a circle receiving this post
return
return
True
return
return
False
In order to quickly retrieve the pages in the desired order, we'll need an index on
user_id
,
month
in both the
social.news
and
social.wall
collections.
>>>
>>>
for
for
collection
iin (
'db.social.news'
,
'db.social.wall'
):
...
collection
.
ensure_index
([
...
(
'user_id'
,
1
),
...
(
'month'
,
-
1
)])
Commenting on a Post
Other than viewing walls and news feeds, creating new posts is the next most common action
taken on social networks. To create a comment by
user
on a given
post
containing the given
text
, we'll need to execute code similar to the following:
from
from
datetime
datetime
import
import
datetime
def
def
comment
(
user
,
post_id
,
text
):
ts
=
datetime
.
utcnow
()
month
=
ts
.
strfime
(
'%Y%m'
)
comment
=
{
'by'
: {
'id'
:
user
[
'id'
],
'name'
:
user
[
'name'
] }
'ts'
:
ts
,
'text'
:
text
}
# Update the social.posts collection
db
.
social
.
post
.
update
(
{
'_id'
:
post_id
},
{
'$push'
: {
'comments'
:
comment
} } )
# Update social.wall and social.news collections
db
.
social
.
wall
.
update
(
{
'posts.id'
:
post_id
},