mongdb总结
#清屏
cls ? --windows#操作數(shù)據(jù)庫
show dbs ? ? --查看所有數(shù)據(jù)庫 use dbName ? ?--使用已有的dbName數(shù)據(jù)庫,或者創(chuàng)建新的數(shù)據(jù)庫dbName,如果一個數(shù)據(jù)庫沒有表就不存在 db ? ? ? ? ? ?--顯示數(shù)據(jù)庫名稱#操作集合
show collections ? ? ? ? ? ? ? ? ? ? ? ?--查詢數(shù)據(jù)庫所擁有的集合 db.createCollecton("collectionName") ? ?--創(chuàng)建集合(表) 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}}) --重新命名字段
#操作索引
#分頁
#排序 ? 1和-1來指定排序的方式,其中1為升序排列,而-1是用于降序排列。
..sort({key:±1}) ? ? ? ? ?--skip(), limilt(), sort()三個放在一起執(zhí)行的時候,執(zhí)行的順序是 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}}]) ? --默認(rèn)會投影_id,不需要_id則設(shè)置_id:0#別名
..aggregate([$project:{key2:"$key1"}}) ?
####查詢
#比較操作符
#邏輯運算符
$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數(shù)據(jù)) ..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函數(shù)#數(shù)組查詢操作
$all ?$elemMatch $size ..find({keys:{$all:[2]}} ?..find({keys:{$all:[[2]]}} ? ? ? ?--數(shù)組中包含所有值 ..find({keys:{$elemMatch:{$lt:4,$gt:1}}) ? ? ? ? ? ? ? ? ? ?--數(shù)組中匹配 ..find({keys:{$elemMatch:{attrA:{$lt:4},attrB:{$gt:1}}}) ..find({keys:{$size:2}}) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--數(shù)組的長度#位查詢操作
$bitAllClear ?$bitAllSet ?$bitAnyClear ?$bitAnySet ..find({a:{$bitsAllClear:[1,5]}}) ? ? ?--第一位和第五位為0(位數(shù)從低到高,從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}) ? ? ? ? ? ? ? ? ? ?--$占位符代表數(shù)組中第一個匹配的元素,投影出該元素 ? ..find({keys:{$elemMatch:{$lt:4,$gt:1}}) ? ? ? ? ? ? ?--將數(shù)組符合條件的第一個元素投影出來..find({keys:value},{keys:{$slice:count}}) ? ? ? ? ? ?--數(shù)組元素裁剪 ..find({keys:value},{keys:{$slice:[skip,limit]}}) ? ? --skip可以為負(fù)數(shù)?
####更新
?
#聚合函數(shù)
$avg $sum(sum,count) $first $last $min $max
?
總結(jié)
- 上一篇: 安卓截长屏怎么操作(安卓截长屏)
- 下一篇: (电脑XP看配置)