如何使用 pymongo 创建 MongoDB 时间序列集合

koy*_*nji 8 python mongodb pymongo

文档展示了如何使用 执行此操作,但是如何在 python 脚本中mongosh创建Time Series Collectionusing呢?pymongo

import pymongo
import time
from datetime import datetime

client = pymongo.MongoClient()
db = client['time-series-db']
col = db['time-series-col']

# ... do something here to make it 'time-series collection' ...

js = {
    "1": "A",
    "2": "B",
    "3": "C",
    "4": "D",
    "5": "E",
}

# create BSON type datetime object needed for 'time-series collection'
ts = time.time()
js['timestamp'] = datetime.utcfromtimestamp(ts)

col.insert_one(js)
Run Code Online (Sandbox Code Playgroud)

pra*_*ad_ 11

你可以试试这个:

conn = pymongo.MongoClient('mongodb://localhost')
db = conn.testDB

db.create_collection('testColl', timeseries={ 'timeField': 'timestamp' })
# - OR -
db.command('create', 'testColl', timeseries={ 'timeField': 'timestamp', 'metaField': 'data', 'granularity': 'hours' })
Run Code Online (Sandbox Code Playgroud)

一般参考:时间序列集合