Sign in
Log inSign up

Any mnongo experts here.Plz help me on building MongoDB news feed schema design for social application

Siraj abbas's photo
Siraj abbas
·Mar 23, 2017

am developing a social application in which user can upload photo albums, update their status and follow other users. I am using Node.js + MongoDB. I have a requirement to display news feeds in which each user gets the update of his followers.

Here is my schema design:

1 profile collection

`{
_id:'p1',
name:'siraj'
}

2 followers collection

{
profileId:2,
followerId:3
}

profile_album collection

{
_id:al1,
ownerId:1,
imageUrl:'google.com'
}

profile_status collection

{
_id:st1,
text:'hey im fine'
}

action collection

{
_id:ac1,
actorId:1,
sourceId:'al1',
sourceType:'album'
}
{
_id:ac1,
actorId:1,
sourceId:'st1',
sourceType:'status'
}

user_feed collection

{
_id:1,
profileId:1,
actionId:ac1
}

I need to get the feed items with profile-status and album details.

Can someone help me to build the query? I tried $lookup but it can select only one document at a time (either the album or status). I need to query both status and album feeds. How to do it if I use separate query and sort in desc order of date.

Here is the aggregation code that I tried:

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
            }
        }
    ])

Thanks in advance.

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct