我正在开发一个社交应用程序,用户可以上传相册,更新其状态并关注其他用户。我正在使用Node.js + MongoDB。我需要显示新闻提要,其中每个用户都可以获得其关注者的更新。
这是我的架构设计:
{
_id:'p1',
name:'siraj'
}
2粉丝收藏
{
profileId:2,
followerId:3
}
{
_id:al1,
ownerId:1,
imageUrl:'www.google.com'
}
{
_id:st1,
text:'hey im fine'
}
{
_id:ac1,
actorId:1,
sourceId:'al1',
sourceType:'album'
}
{
_id:ac1,
actorId:1,
sourceId:'st1',
sourceType:'status'
}
{
_id:1,
profileId:1,
actionId:ac1
}
我需要获取包含个人资料状态和相册详情的Feed项目。
有人可以帮我构建查询吗?我尝试了$lookup
,但它一次只能选择一个文档(相册或状态)。我需要查询状态和相册供稿。如果我使用单独的查询并按日期的desc顺序排序,该怎么做。
这是我尝试的聚合代码:
mongoUtil.db().collection('feed_action').aggregate([
{$match: {actorId: ObjectId(profileId), hidden: false}},
{$sort: {'created_date': -1}},
{$skip: offset}, {$limit: count},
{
$lookup: {
from: "profile",
localField: "actorId",
foreignField: "_id",
as: "actor"
}
},
//{$unwind: '$actor'},
{
$unwind: {
path: "$actor",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "profile_album",
localField: "sourceId",
foreignField: "_id",
as: "profileAlbum"
}
},
{
$unwind: {
path: "$profileAlbum",
preserveNullAndEmptyArrays: true
}
},
{$match: {'profileAlbum.status': 1}},
{
$lookup: {
from: "media",
localField: "mediaId",
foreignField: "_id",
as: "media"
}
},
//{$unwind: '$media'},
{
$unwind: {
path: "$media",
preserveNullAndEmptyArrays: true
}
},
{
$project: {
actor: {
Password: 0,
HashPassword: 0,
ZodiacSign: 0,
RoleId: 0,
Geners: 0,
Interests: 0,
Dob: 0,
CreatedDate: 0,
Gender: 0,
Status: 0,
SystemFileName: 0,
FileName: 0,
Path: 0,
PrivateProfile: 0
},
media: {
_id: 0,
systemFileName: 0,
source: 0,
profileId: 0,
created_date: 0,
updated_date: 0
}
}
},
{
$project: {
action: {
'actionId': '$_id',
'description': '$description',
"source": "$source",
"sourceId": "$sourceId",
"created_date": "$created_date",
'profileAlbum': {
"_id": "$profileAlbum._id",
"profileId": "$profileAlbum.profileId",
"title": '$profileAlbum.title',
"likes": '$profileAlbum.likes',
"comments": '$profileAlbum.comments'
}
}, actor: 1, media: 1, _id: 0
}
}
])
提前致谢。