tunghk_54
tunghk_54

Reputation: 135

Design MongoDb Schema For My Social

I'm new for MongoDB , I just want to create a simple project to test performance of MongoDB The project just like a simple CMS it has users, blogs and comments, users can have friends

so I design my database like that

user
{
     _ID:
    name:
    birth_day:
    sex:

    friends:[id_1,Id_2]
}
blogs
{
    title:
    owner: 
    tags_fiends:
    comments:
    [
        {"_id":"","content":"","date_created":""},
        {"_id":"","content":"","date_created":""},
    ],
    "like"={"_id","_id"}
}

And How many collection are needed for this database. Can I use 1 Collection for both user and blog.Thanks in advance.

Upvotes: 0

Views: 676

Answers (2)

attish
attish

Reputation: 3150

Due to mongoDB is schema less or schema free DB You can make any kind of structure within a document, which is supported:

  • individual elements
  • nested arrays
  • nested documents

There is a couple of things you have to considare during schema design which for it is useful to have the users and the blogs in separated schema. For example if you storing something in a nested array you can specify index for fastening the search within this array, but you can have only one multykéy index (indexed array content) within one particular collection. so if you store, friends and blogs, and posts, and tags all in arrays you can have index only on one of them.

Also important to know in this case that there is a size limit for each document what is now 16MB.

Upvotes: 1

sambomartin
sambomartin

Reputation: 6813

In your scenario, I would make Users a collection and reference it by _id from the blog collection.

In practise, you could make the Blogs an attribute of User, the only constraint being the max doc size of 16MB - but that's a lot of blogs (text).

To get round that (assuming you need to), a separate Blog collection referencing the user _id would be fine. You may need to denormalise the user name too if that's not your _id. This would mean you can get all the blogs for a user in a single query.

Upvotes: 0

Related Questions