1

I have the below structure

{
    "_id": {
        "$oid": "55ae24016fb73f6ac7c2d640"
    },
    "Name": "some name",
    "District": "some district",
    "Stories": [
        {
            "userId": "105304831528398207103",
            "story": "some story",
            "Likes": ["user id 1" ..... ],
            "_id": {
                "$oid": "55c055af1875b0002572cf94"
            }
        }
    ]
}

So in this requirement there will be many stories, and userId is the id of the author who posted it. And there is a column called Likes, and I will add user id of user who likes the story. And I remove their id from Likes array when they unlike it.

What is a better model for this requirement? or is this model fine for storing likes?

Is it ok to use nested arrays in mongodb ?

1 Answer 1

1

Yes, this can be a good approach for you. Nested arrays are OK to use, maybe is the best solution for you in this case, to embed the likes in the stories. In this way, when you query for a story you will get also all it's likes(very fast). The drawback is that it will be more difficult to query for a specific 'like', without having a 'like' collections. It will be more difficult to query for all likes from a user.

On extent, you can also make an array of embedded documents out of stories, storing the id and the name of the user. In this way, the name field is denormalized, but it will be more fast to get the name of the user who voted the story, without the need to make an extra query to the user collection.

..
"Stories": [
    {
        "userId": "105304831528398207103",
        "story": "some story",
        "Likes": [
                 {user_id :"user id 1", user_name: "user name"}, 
                 ..... 
             ],
        "_id": {
            "$oid": "55c055af1875b0002572cf94"
        }
    }
]
..
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.