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索引
DBCollection dbc = db.getCollection("role")dbc.setHintFields(null)
虽然3.0以后版本中 DBCollection方法 已过时,可以将就用
直接用 $hint *** 作符
db.users.find( { name: {}, $hint: { age : 1 } } )
参考地址
https://docs.mongodb.org/manual/reference/operator/meta/hint/#metaOp._S_hint
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)