I have a time series that grows and is (potentially) revised through time:
on "2013-01-01": First version of the data
"2013-01-01" 10
on "2013-01-02": Data of the 1st of Jan is revised from 10 to 11
"2013-01-01" 11
on "2013-02-01": First version of the data of the 1st of Feb
"2013-01-01" 11
"2013-02-01" 20
on "2013-02-02": Data of the 1st of Feb is revised from 20 to 21
"2013-01-01" 11
"2013-02-01" 21
most frequent queries:
query1: get the most recent version of all dates
"2013-01-01" 11
"2013-02-01" 21
query2: get the time series as it was known at a certain date:
For instance, querying with "2013-02-01", I need to get
"2013-01-01" 11
"2013-02-01" 20
Note that query1 is a the same as query2 but with date = current date
I need help to structure my documents, and as I come from a relational background, I am not sure about the implications of my structure. I have basically identified 2 possible structure, and would be happy to have some feedbacks, or suggestions of other structure.
OPTION A: Each revision in a separate document
{
"id":"1",
"date":"2013-01-01",
"version_date":"2013-01-01",
"value":10
}
{
"id":"1",
"date":"2013-01-01",
"version_date":"2013-01-02",
"value":11
}
{
"id":"1",
"date":"2013-02-01",
"version_date":"2013-02-01",
"value":20
}
{
"id":"1",
"date":"2013-02-01",
"version_date":"2013-02-02",
"value":21
}
OPTION B: One document contains all the revisions of one date
{
"id":"1",
"date":"2013-01-01",
"values" : [
{ "version_date":"2013-01-01",
"value":10
},
{
"version_date":"2013-01-02",
"value":11
}
}
{
"id":"1",
"date":"2013-02-01",
"values" : [
{ "version_date":"2013-02-01",
"value":20
},
{
"version_date":"2013-02-02",
"value":21
}
}
In option B, I am also concerned by the fact that it might be a bit more difficult to perform the update query as the document has a growing part, which i am not sure is very well supported by / optimised for mongodb
EDIT: I am also considering option C to speed up query1: (although it might slow down a bit the writing)
{
"id":"1",
"date":"2013-01-01",
"values" : [
{ "version_date":"2013-01-01",
"value":10
},
{
"version_date":"2013-01-02",
"value":11
}
"last_value":11
}
{
"id":"1",
"date":"2013-02-01",
"values" : [
{ "version_date":"2013-02-01",
"value":20
},
{
"version_date":"2013-02-02",
"value":21
}
"last_value":21
}