#清屏
cls --windows
#操作数据库
show dbs --查看所有数据库
use dbName --使用已有的dbName数据库,或者创建新的数据库dbName,如果一个数据库没有表就不存在
db --显示数据库名称
#操作集合
show collections --查询数据库所拥有的集合
db.createCollecton("collectionName") --创建集合(表)
db.collectionName.drop() --删除集合(表)
db.collection.renameCollection("newCollectionName") --重新命名集合(表)
#操作文档
db.collectionName.insert({key:value,...}) --插入文档(行)
db.collectionName.insertOne({key:value,...})
db.collectionName.insertMany( [ <document 1> , <document 2>, ... ])
db.collectionName.save({key:value,...}) --插入或更新文档(行)
db.collectionName.find([{key:value,...}]) --查询文档(行)
db.collectionName.findOne({key:value,...}) --查询单个文档(行)
#db.collectionName.remove({key:value,...}[,option]) --过时的删除文档方法
db.collectionName.deleteOne({key:value,...}) --删除一个文档
db.collectionName.deleteMany({key:value,...}) --删除多个文档
db.collectionName.update({key:value,...},{key:value,...}[,option]) --替换文档
db.collectionName.update({key:value,...},{$set:{key:value,...}}[,option]) --更新字段
db.collectionName.update({key:value,...},{$rename:{key:newKey}}) --重新命名字段
#操作索引
db.collectionName.createIndex({key:±1,...}[,options]) --创建索引
#db.collectionName.ensureIndex(keys[,options]) --创建索引,从版本3.0.0开始不推荐使用
db.collectionName.reIndex([keys[,options]]) --重建索引db.collectionName.getIndexes() --查看集合所有索引
db.collectionName.totalIndexSize() --查看集合索引的总大小
db.system.indexes.find() --查看数据库的索引
db.collectionName.dropIndex("INDEX-NAME")
db.collectionName.dropIndexes()
#分页
..skip(index).limit(size) --分页 index从0开始
#排序 1和-1来指定排序的方式,其中1为升序排列,而-1是用于降序排列。
..sort({key:±1}) --skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是 sort()>skip()>limit()
#最大值、最小值
..sort({key:±1}).limit(1)
..aggregate([{$sort:{a:-1}},{$limit:1}])
#投影
..find({}, {key1:1,key2:0}) --1用于显示字段,而0用于隐藏字段。
..aggregate([{$project:{key1:1,key2:0}}]) --默认会投影_id,不需要_id则设置_id:0
#别名
..aggregate([$project:{key2:"$key1"}})
####查询
#比较操作符
$lt $gt $eq $ne $lte $gte $in $nin
..find({key : {$gt : 100}})
#逻辑运算符
$or $and $not $nor(!$not)
..find({$or:{key:value,..},..})
#元素查询运算符
$type $exist
..find({key:{$type:2}})
或
..find({key:{$type:'string'}})
#求值查询
$expr $mod $regex /../ $text $where $jsonSchema(验证json数据)
..find({$expr:{$gt:["$key1","$key2"]}}) --表达式查询
..find({key:{$mod,[4,1]}}) ..find({key:{$mod:[4,{$ne:1}]}}) --求余
..find({key:{$regex:/../,$options:'imxs'}) 或 ..find({key:/教/}) --正则表达式
..find({$text:{$search:"apple"}}) --在索引行查询字符串
..find({$where: function() { return this.key == 2;}}); --where函数
#数组查询操作
$all $elemMatch $size
..find({keys:{$all:[2]}} ..find({keys:{$all:[[2]]}} --数组中包含所有值
..find({keys:{$elemMatch:{$lt:4,$gt:1}}) --数组中匹配
..find({keys:{$elemMatch:{attrA:{$lt:4},attrB:{$gt:1}}})
..find({keys:{$size:2}}) --数组的长度
#位查询操作
$bitAllClear $bitAllSet $bitAnyClear $bitAnySet
..find({a:{$bitsAllClear:[1,5]}}) --第一位和第五位为0(位数从低到高,从0开始计算)
..find({a:{$bitsAllClear:34}}) --00100010
..find({a:{$bitsAllSet:[1,5]}}) --第一位和第五位为1
..find({a:{$bitsAnyClear:[1,5]}}) --第一位或第五位为0
..find({a:{$bitsAnySet:[1,5]}}) --第一位或第五位为1
#备注
$comment
..find( { <query>, $comment: <comment> } )
#投影操作
$ $elemMatch $meta $slice
..find({keys:{$lt:2},{"keys.$":1}) --$占位符代表数组中第一个匹配的元素,投影出该元素
..find({keys:{$elemMatch:{$lt:4,$gt:1}}) --将数组符合条件的第一个元素投影出来..find({keys:value},{keys:{$slice:count}}) --数组元素裁剪
..find({keys:value},{keys:{$slice:[skip,limit]}}) --skip可以为负数
####更新
#聚合函数
$avg $sum(sum,count) $first $last $min $max