mongo数据库是怎么创建索引

mongo数据库是怎么创建索引,第1张

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索引

从Robo 3T可视化界面中,去创建mongodb数据表的索引。

1.数据表结构:{_id:value,   doc_city:value,    doc_province:value,   content,   judgementId}

2.content字段展开:

3.对content字段里的title/caseType/judgementType建立索引

db.getCollection('chongqing').createIndex({"content.title":1, "content.judgementType":1, "content.caseType":1})

4.让创建索引的过程在后台运行

db.getCollection('chongqing').createIndex({"content.title":1, "content.judgementType":1, "content.caseType":1},{background:true})

5.查询集合索引

db.getCollection('chongqing').getIndexes()

6.查看索引集合大小

db.getCollection('chongqing').totalIndexSize()

7.删除集合所有索引

db.getCollection('chongqing').dropIndexes()

8.删除集合指定索引

db.getCollection('chongqing').dropIndex('索引名')

1.连接mongodb

mongo/bin目录下执行

./mongo

2.查看数据库

show dbs

3.查看当前所在数据库

db

4.创建数据库

use openfire (临时创建 如果不做 *** 作 则离开后被系统删除)

5.在当前数据库删除当前数据库

db.dropDatabase()

6.查看当前库的所有用户

show users

7.查看集合(或者叫表)

show collections

8.创建集合

db.createCollection(“mycollection”)

9.创建集合并制定集合的属性

db.createCollection(“mycol”, { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )

字段

类型

描述

cappedBoolean(可选)如果为true,则启用封顶集合。封顶集合是固定大小的集合,会自动覆盖最早的条目,当它达到其最大大小。如果指定true,则需要也指定尺寸参数。

autoIndexIDBoolean(可选)如果为true,自动创建索引_id字段的默认值是false。

sizenumber(可选)指定最大大小字节封顶集合。如果封顶如果是 true,那么你还需要指定这个字段。

maxnumber(可选)指定封顶集合允许在文件的最大数量。

10.另一种创建集合

(当插入一条数据时,不存在mongodb会帮我们创建,和创建数据库同理)

db.MySecondCollection.insert({“name” : “ming”})

11.删除当前库的一个集合

db.CollectionName.drop()

12.插入一条数据

db.MyFirstCollection.insert({“_id”:”3”,”title”:”mongotest”,”description”:”this is test”})

注意:插入的都是JSON形式的,所以一定要用{},否则会报错:

Sat Mar 19 14:22:39.160 SyntaxError: Unexpected token :

13.插入一条_id存在的数据

db.MyFirstCollection.insert({“_id”:”3”,”title”:”mm”})

输出:E11000 duplicate key error index: openfire.MyFirstCollection.$_id_ dup key: { : “3” }

解释:_id即是mongodb的默认主键,默认自动生成,我们可以直接设置以达到我们想要的目的


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7561846.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存