I am trying to figure out time series using MongoDB. A common decision made by the community is to use subdocuments to store information at different levels of detail (see Schema schema for time series data in MongoDB ).
For example, look at the following document:
{
timestamp_minute: ISODate("2013-10-10T23:06:00.000Z"),
type: "memory_used",
values: [
999999, // 1 second
β¦
1000000, // nth second
1500000, // n+1th second
β¦
2000000 // 60th
]
}
The document is indexed by minute information and contains a subdocument that stores more detailed information for every second.
So far so good. This approach requires optimization for proper operation:
Another optimization [..] pre-distributes all documents for the upcoming period of time; This never causes an existing document to grow or move to disk.
, $setOnInsert update.
db.getCollection('aCollection').update(
{
timestamp_minute: ISODate("2013-10-10T23:06:00.000Z"),
type: "memory_used"
},
{
$setOnInsert: { values: {'0': 0, '1': 0, '2': 0}},
$inc: {"values.30": 1}
},
{ upsert: true }
)
, . istruction :
Cannot update 'values' and 'values.30' at the same time
.
: - ? , , , ( type.
.