Please help me to design the Collection Schema and Its Scope

Hello @all,
I have designed a collection schema to store my campaign’s visitors counts and also save its count as per visiting date to show the graphical analytics. Here is my schema design…

Schema({
        userId : { type: Schema.Types.ObjectId , ref: 'users'},
        title : { type: String },
        slug : { type: String },
        usedTemplate : { type: String },
        status :{ type: boolean, default : 1},
        campaignsData : { type: Object, default: {} },
        visits : { type: Number, default : {}},
        addedOn : { type: Date, default: Date.now },
    });

and I have decided to store visits as

visits : {
    totalVisits : 5,
    visitDetails : {
        "2022-03-01" : 1,
        "2022-03-02" : 3,
        "2022-03-03" : 1,
    }
}

My queries are:

  1. Is it right schema to do same?
  2. If there are lots of records (assume 2 or 3 years of records it will be 365*3 records in “visits.visitDetails” keys) in my “visits” key will this effect on my query execution?

Hi @Praduman_Tiwari ,

Well I suggest that you read our design best practices materials and antipattern.

Having an embedded object in one document with 800+ fields does not sound as a good design.

First it will create large documents which seems unnecessary. Second you won’t be able to filter on a field name …

I would suggest to use 2 collections :

campaigns collection

Schema({
userId : { type: Schema.Types.ObjectId , ref: ‘users’},
campaignId : { type: Schema.Types.ObjectId},
title : { type: String },
slug : { type: String },
usedTemplate : { type: String },
status :{ type: boolean, default : 1},
campaignsData : { type: Object, default: {} },
addedOn : { type: Date, default: Date.now },
})

visits collection

Schema({
visitDate : { type: Date },
campaignId : { type: Schema.Types.ObjectId, ref: ‘campaings’},
visits : { type: Number }
})

Now you can index campaignId : 1 , visitDate : 1 together to get different visit information for plot and analysis :slight_smile:

Here are links to the things we talked about :

Thanks
Pavel

2 Likes

Hi @Pavel_Duchovny,
Thank you so much for your valuable reply :slightly_smiling_face:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.