python mongodb 创建库与创建索引

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40294332/article/details/84582452

初始话链接

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')

猜你喜欢

转载自blog.csdn.net/weixin_40294332/article/details/84582452