1. 程式人生 > >python mongodb 建立庫與建立索引

python mongodb 建立庫與建立索引

初始話連結

import pymongo
import datetime

#mongodbUrl = 'mongodb://test:[email protected]:27018,192.168.1.3:27018,192.168.1.3:27018'
mongodbUrl="mongodb://localhost:27017/"
dbName = "test"
myclient = pymongo.MongoClient(mongodbUrl)
mydb = myclient[dbName]

建立表並初始話索引

如果不指定索引名稱,預設使用index語句為索引名稱,如果索引過長會報

pymongo.errors.OperationFailure: exception: namespace name generated from index name "test.table01.${"imei":1,"vin":1,"startTime":1,"startTime":-1,"endTime":1,"endTime":-1,"channelld":1,"status":1}_1" is too long (127 byte max)

指令碼

table01= mydb["table01"]
table01.create_index('{"imei":1,"vin":1,"startTime":1,"startTime":-1,"endTime":1,"endTime":-1,"channelld":1,"status":1}', name='table01_index')