I suggest a single time series entry per document. There are some problems with storing multiple entries per document:
- a single document is limited to a certain size (currently 16 MB); this limits how many entries can be stored in a single document
- as more entries are added to a document, the entire document (and time series) will needlessly be deleted and reallocated to a larger piece of memory
- queries on sub-documents are limited compared to queries on regular documents
- documents with very flat structures (like one sub-document for each second) are not performant
- the built-in map-reduce does not work as well on sub-documents
Also note a timestamp is built-in to the default MongoDB ObjectId. You can use this if the time series precision is less than one second.
Here is an example BSON document from an event logging library that uses MongoDB:
Example format of generated bson document:
'timestamp': Timestamp(1290895671, 63),
'message': 'test message',
'stackTrace': 'Traceback (most recent call last):
File "/var/projects/python/log4mongo-python/tests/test_mongo_handler.py", line 36, in test_emit_exception
Since an event log is similar to a time series, it may be worth studying the rest of the code. There are versions in Java, C#, PHP, and Python.
Here is another similar open source project: Zarkov