Jits
Jits

Reputation: 545

MongoDB Schema optimization for contacts

I am storing my contacts in mongodb like this but main drawback of this schema is I am not able to store 40k-50k contacts in one document due to limit of 16mb.

I want to change my schema now. So can anyone please suggest me best way to redesign this.

Here is my sample doucument

{
    "_id" : ObjectId("5c53653451154c6da4623a77"),
    "contacts" : [{
         name:"",
         email:"",
         group:[5c53653451154c6da4623a79]
    }],
    "groups" : [{
         _id: ObjectId("5c53653451154c6da4623a79"),
         group_name:"test"
     }],
}

Upvotes: 0

Views: 126

Answers (1)

Caconde
Caconde

Reputation: 4483

According to you document sample, contacts belongs to a group.

In that scenario, there are different ways to end up with a better schema:

1- Document embedding:

You will have an array of contacts inside each group document.

collection groups:

{
    "_id": ObjectId("5c53653451154c6da4623a79"),
    "group_name":"test",
    "contacts": [
        {
            "name":"something",
            "email":"something",
        },         
        {
            "name":"something else",
            "email":"something else",
        }
    ]
}

2- Document referencing:

You will have two collections - contacts and groups - and store a group reference inside each contact.

collection contacts:

{
    "_id" : ObjectId("5c53653451154c6da4623a77"),
    "name":"something",
    "email":"something",
    "groups":["5c53653451154c6da4623a79"]
},
{
    "_id" : ObjectId("5c536s7df9sd7f987d9s7d98"),
    "name":"something else",
    "email":"something else",
    "groups":["5c53653451154c6da4623a79"]
}

collection groups:

{
    "_id": ObjectId("5c53653451154c6da4623a79"),
    "group_name":"test"
}

Why are we referencing group inside contact and not the contrary? Because we probably will have more contacts than groups. This way we have smaller documents with smaller "reference arrays".

The path you will follow depends a lot on how many contacts you have per group. If this number is small, I would take the Document Embedding approach, for the sake of simplicity and access easiness. If you have a large number of contacts per group, I would use Document Reference, to have smaller documents.

Upvotes: 1

Related Questions