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:
Is it right schema to do same?
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
Here are links to the things we talked about :
Thanks
Pavel
2 Likes
Hi @Pavel_Duchovny ,
Thank you so much for your valuable reply
system
(system)
Closed
April 2, 2022, 10:20am
4
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.