2种方式:
(1)mongo shell
db.yourCollectionName.createIndex({fieldName: 1})
举例:
db.gameShortLink.createIndex({shortLink: 1})
(2)pymongo代码
indexKeyList = [
("fieldName", pymongo.ASCENDING),
]
mongoCollection.create_index(indexKeyList)
注:
import pymongo
from pymongo import IndexModel
# from pymongo import ASCENDING, DESCENDING
indexShortLink = IndexModel([("shortLink", pymongo.ASCENDING)], name="shortLink")
indexIsParseOk = IndexModel([("parsedLink.isParseOk", pymongo.ASCENDING)], name="parsedLink_isParseOk")
indexErrType = IndexModel([("parsedLink.errType", pymongo.ASCENDING)], name="parsedLink_errType")
indexRealGameName = IndexModel([("parsedGame.realGameName", pymongo.ASCENDING)], name="parsedGame_realGameName")
indexGameTheme = IndexModel([("parsedGame.gameTheme", pymongo.ASCENDING)], name="parsedGame_gameTheme")
indexModelList = [
indexShortLink,
indexIsParseOk,
indexErrType,
indexRealGameName,
indexGameTheme,
]
mongoCollectionShortlink.create_indexes(indexModelList)
详见:(百度搜)
【已解决】用mongo的shell给MongoDB创建索引以提高查询速度
【已解决】PyMongo中如何一次性创建多个index索引
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)