The documentation shows how to do it with mongosh
, but how do you create Time Series Collection
using pymongo
from within a python script?
JavaScript
x
24
24
1
import pymongo
2
import time
3
from datetime import datetime
4
5
client = pymongo.MongoClient()
6
db = client['time-series-db']
7
col = db['time-series-col']
8
9
# ... do something here to make it 'time-series collection' ...
10
11
js = {
12
"1": "A",
13
"2": "B",
14
"3": "C",
15
"4": "D",
16
"5": "E",
17
}
18
19
# create BSON type datetime object needed for 'time-series collection'
20
ts = time.time()
21
js['timestamp'] = datetime.utcfromtimestamp(ts)
22
23
col.insert_one(js)
24
Advertisement
Answer
You can try this:
JavaScript
1
7
1
conn = pymongo.MongoClient('mongodb://localhost')
2
db = conn.testDB
3
4
db.create_collection('testColl', timeseries={ 'timeField': 'timestamp' })
5
# - OR -
6
db.command('create', 'testColl', timeseries={ 'timeField': 'timestamp', 'metaField': 'data', 'granularity': 'hours' })
7
General Reference: Time Series Collections