mongodb常用命令(一)
文章目录
1. 数据查询
-
查询collection文档中所有数据
db.collection.find()
-
查询collection文档中一条数据
db.collection.findOne()
-
查询collection文档中想要显示的字段, 表中自带_id默认会显示。0 为不显示,1 为显示
db.collection.find({},{_id: 0, x: 1, y : 1})
-
随机查询 n 条collection文档中数据
db.collection.aggregate([{$sample: {size: n}}])
-
随机查询 n 条collection文档中限定字段
db.collection.aggregate([{$match: {x: ''}}, {$sample:{size: n}}])
-
查询collection指定数量数据,skip表示跳过n条数据,limit表示返回m条数据
db.collection.find().skip(n).limit(m)
-
对collection文档中x字段进行排序,1表示升序排列,-1表示降序排列
db.collection.find().sort({x: 1})
mongodb条件操作符
$regex 模糊查询(正则表达式)
-
查询collection文档中 x 字段中包含 xxx 字符的文档
db.collection.find({x: {$regex: 'xxx'}})
$all 匹配所有
db.collection.find({x: {$all: [ ]}})
$exists 字段是否存在(存在true显示数据,不存在则不显示)
db.collection.find({x: {$exists: true}})
$size 数组元素个数(用来查询特定长度的数组)
db.collection.find({x: {$size: n}})
$mod 取模运算(查询x取模10等于0的数据)
db.collection.find({x: {$mod: [10, 0]}})
$ne 不等于(x值不等于n的数据)
db.collection.find({x: {$ne: n}})
$in 包含(x值包含[ ]中数字的数据) | $nin 不包含(用法一样)
db.collection.find({x: {$in: [2, 3, 4]}})
-
2.数据表中数据查重
-
db.collection.aggregate([{$group: {_id: {x: '$x'}, count: {$sum:1}}}, {$match: {count: {$gt: 1}}}])
查询collection表中x字段数据大于1的所有信息
mongodb默认10分钟内处理完数据,当超时时,则抛出cursor not found异常。当数据量大时,处理数据超过十分钟,需要延长超时时间,可添加noCursorTimeout()参数。但是不推荐使用,会导致没有正确关闭游标。
可用cursorTimeoutMillis:600000(default:10minutes)设置需要延长的时间参数
-
db.runCommand({distinct: 'clothes_textures', key: 'reg_id'}).values.length
; 查询reg_id去重后的数据。使用这种查询方式,当结果集大于16M时会报错,不适用于大数据统计。 -
使用聚合函数aggregate,返回统计数量
db.collection.aggregate([{$group:{_id:'$field'}},{$group:{_id:null, count:{$sum:1}}}])
-
3.表中重复数据去重
-
db.collection.aggregate([{$group: {_id: {x: '$x'}, count: {$sum: 1}, dups: {$addToSet: '$_id'}}}, {$match: {count: {$gt:1}}}], (1)).forEach(function(doc){doc.dups.shift(); db.collection.remove({_id: {$in: doc.dups}});})
-
$addToSet 表示在返回结果数组中增加_id字段
-
doc.dups.shif() 表示从数组第一个值开始删除,用来删除重复的一个_id,让后面的删除语句不会删除所有的数据
-
forEach 根据_id循环删除数据
-
当文档超过16M时,命令运行会出错,可在(1)处添加{allowDiskUse: true}, 表示使用磁盘
4.mongoimport 文件导入
-
mongoimport --host hostname:port --username usernane --password password --db database --collection collection --type csv/json/tsv --headerline --file filename
-
–host 连接数据库的hostname和端口号,默认连接本机,端口号默认27017
-
–username/–password 连接数据库的用户名和密码,没有设置可不写
-
–db 连接的数据库,不存在则自动创建
-
–collection 导入数据的文档,不存在则自己创建,不写的话,则默认导入文件的文件名
-
–type 导入文件的类型,默认json类型
-
–headerline 导入文件的表头,存入文档后为字段名,只有csv文件和tsv文件有 headerline,json没有
-
–file 导入文件的路径
-
–ignoreBlanks 忽略空文档,仅用于csv和tsv
-
–drop在插入文档之前删除集合
-
–jsonArray 导入json文件时的参数,将输入源视为json数组
5.mongoexport 文档导出
-
–host
-
–username/–password
-
–db
-
–collection
-
–fields [, ] 选择导出文档的字段
-
–type 导出文档的类型(json或csv),默认json。csv导出时需要指定导出的字段名。
-
–out filename 导出文档的名字,若未指定,则默认为stdout
-
–jsonArray 导出文档为json时的参数
-
–noheaderline 导出csv数据,第一行没有字段名列表
mongoexport --host 192.168.1.xxx:27017 --db database --collection collection --type csv -f reg_id,pic_uuid,task_uuid --out /data/clothes_region_merge.csv
6. 数据库/表重命名
-
db.copyDatabase('oldname', 'newname')
-
数据库删除 use oldname db.dropDatabase()
db.collection.renameCollection('newname') //集合重命名
7. 创建索引
-
单字段索引
。
db.collection.createIndex({field: 1})
。对field字段建立索引,1 表示升序, -1 表示降序序,对于单字段索引, 升序降序效果一样
当建索引的字段长度超过1024大小时,会报错:WiredTigerIndex::insert: key too large to index,即键太大无法创建索引。这时可以选择忽略错误或建立hash索引或text索引。
。忽略错误 在mongodb的 mongod.conf文件中配置参数
--setParameter failIndexKeyTooLong=false
-
复合索引
。
db.collection.createIndex({field1: 1, fiels2: 1, ...})
。针对多个字段联合创建索引,先按第一个字段排序,第一个字段相同的 文档按第二个字段排序,以此类推。字段顺序也会影响索引的顺序,实 际操作时可根据创建索引字段选择字段顺序
-
多key索引
。当索引的字段为数组时,创建的索引为多key索引,多key索引会为数组 的每个元素建立索引
-
文本索引
。
db.collection.createIndex({field: 'text'})
。此索引允许对包含字符串内容的所有字段进行文本搜索。如果不清楚要 在文本索引中包含哪些字段或用于特殊查询,那么对于高度非结构化的数据,这样的索引可能很有用。
-
哈希索引
。
db.collection.createIndex({field: 'hashed'})
。指按照某个字段的hash值建立索引,目前主要用于[MongoDB Sharded Cluster](https://yq.aliyun.com/articles/32434?spm=5176.100238.yqhn 2.22.0cUwgh)的Hash分片,hash索引只能满足 字段完全匹配的查询,不能满足范围查询等。