Using Mongodb time series collection for survey data

Viewed 265

We are using Mongo DB and considering using the time series collection for storing our individual survey response results.

Is a time series the right kind of collection since we want to be able to query the survey responses to show analytics between 2 dates?

I'm trying to understand the best way to understand the granularity feature. Sometimes data will come in every second, whereas sometimes data will come in minutes or hours depending on the time of day. When querying though, we never need to query the data with a granularity more precise than hours. What would be the best way to set the granularity in this case? I was originally thinking hours since that what we would query on, but data may come in every few seconds at times.

Finally, with regards to the metaField is there any specific data that shouldn't be stored in the metafield? is three a cost associated with storing things in the metaField? Should I add any fields in there that I may want to index on in the future?

For example, the companyId in a survey response could just be part of the value, or you could consider it metaField that you would want to index on (to get all responses related to a specific company). But at the other extreme, I assume you wouldn't want to use the survey answer as a metadata field. Another example would be a user, some survey responses would have a user attached to them, most of the time unique, but there may be multiple responses from the same user and you might want to get all results between x and y where the response was from a specific user.

I guess the high level question to the metaField question is, is there any reason not to put all the data relating to a "measurement" into the metaField object?

This is the current Schema:

const responseSchema = mongoose.Schema({
metadata: {
    survey: {type: mongoose.Schema.Types.ObjectId, ref: "survey"},
    company: {type: mongoose.Schema.Types.ObjectId, ref: "company"}

},
answers: {type: Map, of: mongoose.Mixed},    
user: {type: mongoose.Schema.Types.ObjectId, ref: "user"},
customerName: String,
phone_numer: String,
email : String,
createdAt: Date
}, { timeseries: {
timeField: 'createdAt',
metaField: 'metadata',
granularity: 'seconds'
}})
0 Answers
Related