MongoDB 是一個基于分布式檔案存盤的資料庫,由 C++ 語言撰寫,旨在為 WEB 應用提供可擴展的高性能資料存盤解決方案,是一個介于關系資料庫和非關系資料庫之間的產品,是非關系資料庫當中功能最豐富,最像關系資料庫的,
- 指定文本編輯器
EDITOR="\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\""
- 創建資料庫
use docname
- 查看所有資料庫
show dbs
- 查看當前資料庫或集合db插入記錄
db.docname.insert({key:value,key:value,...})
- 查看集合
show collections
- 查看當前操作集合及檔案
db.docname
- 查看所有記錄
db.colname.find()
- 查看單條記錄
db.colname.findOne()
- 修改記錄
db.colname.update({key:value},{$set:{key:value,...}})
- 洗掉集合
db.colname.drop()
- 洗掉檔案
db.dropDatabases()
- 查看當前資料庫引數
db.stats()
- 幫助
db.help()
- 命名db.getCollection("db-name").insert({...})
直接執行function insert(object){db.getCollection("colname").insert(objection)}
呼叫insert({key:value,...})
修改器指定一個鍵值對,存在鍵就修改,不存在就創建
$set{$set:{field:value}}
- 為指定鍵的數字型別數值進行加減操作
\(inc{\)inc:{field:value}}
- 洗掉指定鍵
\(unset{\)set:{field:1}}
- 指定鍵是陣列就追加新的數值指定鍵不是陣列則中斷當前操作指定鍵不存在則創建陣列型別的鍵值對
\(push{\)push:{field:value}}
- 批量添加資料
\(pushAll{\)pushAll:{field:array}}
- 目標陣列存在此項則不操作,不存在則添加
\(addToSet{\)addToSet:{field:value}}
- 從指定陣列洗掉資料,1洗掉最后一個,-1洗掉第一個
\(pull{\)pull{field:value}}
- 批量洗掉
\(pullAll{\)pullAll{field:array}}
- 陣列定位器,只對陣列中部分進行操作
{ "_id" : 4, "books" : [ { "type" : "js", "name" : "extjs4" }, { "type" : "js", "name" : "jquery" }, { "type" : "db", "name" : "mongodb" } ] }
db.student.update({"books.type":"js"},{\(set:{"books.\).count":152}})
- 洗掉記錄
db.colname.remove({field:value})
$addToSet與$each結合批量陣列更新
db.colname.update({field:value},{$addToSet:{field:{$each:[value,...]}}})
runCommand可以執行MongoDB中的特殊函式
findAndModify(特殊函式)回傳update或remove后的檔案
runCommand({"findAndModify":"processes",query:{查詢器},sort{排序},new:trueupdate:{更新器},remove:true})
EG:ps = db.runCommand({findAndModify:"student",query:{name:"maxkim"},update:{$set:{age:234}},new:true})
- 查看回傳值
ps.value
- 查詢顯示 1,不顯示 0回傳指定列
db.colname.find({field:{$...:...}},{_id:0, name:1,...})
$gte 大于等于
$gt 大于
$lte 小于等于
$lt 小于
$in 任意引數在參考集合里,則匹配
db.c2.find({age:{$in:[1,2,3]}})
-
$nin (not in)所有引數都不在參考集合里,則匹配
-
$all 所有引數在參考集合里,則匹配
-
$or 有一個條件匹配就成立
db.c2.find({$or:[{age:{$lt:5}},{age:{$gt:999995}}]})
-
$ne 不匹配引數條件
-
$not 不匹配結果
-
$exists 判斷元素是否存在
-
$slice 選擇回傳檔案的子集
db.product.find({},{reviews:{$slice:12}})
- 回傳前12條
db.product.find({},{reviews:{$slice:-5}})
- 回傳后5條
正則查詢
db.colname.find({field:/li/i},{field:0,...})
db.review.find({text:/best|worst/i})
db.review.find({text:{$regex:"best|worsst",$options:"i"}})
- 索引
db.colname.find({field.n:value})$size
- 指定陣列長度
db.persons.find({books:{$size:4}},{_id:0, books:1})
- 添加欄位
db.persons.update({},{$set:{field:n}},false,true)
- 改變更新方式
db.persons.update({},{$push:{field:"value"},$inc:{field:1}})
- 索引1為正序 -1為倒序
db.books.ensureIndex({number:1})
- 索引命名
db.books.ensureIndex({name:1},{name:"new_name"})
- 唯一索引
db.books.ensureIndex({name:-1},{unique:true})
- 剔除重復值
db.books.ensureIndex({name:-1},{unique:true,dropDups:true})
- 使用指定索引查詢
db.books.find({name:"",number:1}).hint({name:-1})
- 查看查詢使用索引及查詢資料狀態資訊(分析查詢性能)
db.books.find({name:"",number:1}).explain
- 洗掉索引
db.runCommand({dropIndexes:"books", index:"name_1"})
- 洗掉所有索引
db.runCommand({dropIndexes:"books", index:"*"})
- 匹配子檔案.
db.users.find({'addresses.name':"home"}).pretty()
$elemMatch
- 所有詞語在相同的子檔案中,則匹配
db.users.find({addresses:{$elemMatch:{name:"home",state:"NY"}}})
- $size 子檔案陣列大小與提供的文本值相同,則匹配
db.users.find({address:{$size:3}})
- $where 執行任意JavaScript來選擇檔案
db.reviews.find({$where:"this.helpful_votes>3"})
2D查詢資料
var map = [{ "gis" : { "x" : 185, "y" : 150 }},{ "gis" : { "x" : 70, "y" : 180 }},{ "gis" : { "x" : 75, "y" : 180 }},{ "gis" : { "x" : 185, "y" : 185 }},{ "gis" : { "x" : 65, "y" : 185 }},{ "gis" : { "x" : 50, "y" : 50 }},{ "gis" : { "x" : 50, "y" : 50 }},{ "gis" : { "x" : 60, "y" : 55 }},{ "gis" : { "x" : 65, "y" : 80 }},{ "gis" : { "x" : 55, "y" : 80 }},{ "gis" : { "x" : 0, "y" : 0 }},{ "gis" : { "x" : 0, "y" : 200 }},{ "gis" : { "x" : 200, "y" : 0 }},{ "gis" : { "x" : 200, "y" : 200 }}]for(var i = 0;iprev.m){
prev.m = doc.m;
prev.name = doc.name;
prev.country = doc.country;
}
},
condition:{m:{$gt:90}} }}
)
-
固定集合
-
創建一個1000個位元組的固定集合,可以存盤10個檔案
db.createCollection("MyColl",{size:1000,capped:true,max:10})
- 普通集合轉換固定集合
db.runCommand({convertToCapped:"persons",size:100000})
- 反向排序,默認插入順序
db.MyColl.find().sort({$natural:-1})
GridFS
- 上傳檔案
mongofiles -d colname -l "localfile" put "filename"
- 查看所有檔案
mongofiles -d test list
- 洗掉檔案
mongofiles -d test delete 'filename'
- Eval(啟用)
db.eval("function(name){return name;}","uspcat")
- 匯出
mongoexport.exe -d test -c persons -o "D:\MongoDB\log\dump.json"
- 匯入
mongoimport.exe --db test --collection persons --file "D:\MongoDB\log\dump.json"
- 運行時備份
mongodump.exe --host 127.0.0.1:27017 -d test -o "D:\MongoDB\log\test"
- 運行時恢復
mongorestore.exe --host 127.0.0.1:27017 -d test "D:\MongoDB\log\test.json\test\"
fsync
- 上鎖(use admin)
db.runCommand({fsync:1, lock:1})
- 上鎖
db.currentOp()
- 資料恢復
db.repairDatabase()
- 添加用戶
db.createUser({user:"pangdahai",pwd:"123",roles:
[{role:"readWriteAnyDatabase",db:"admin"}]})
Successfully added user: {
"user" : "quanee",
"roles" : [
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
}
]
}
-
添加權限認證
-
auth
db.auth("username","password")
- 洗掉用戶
db.system.users.remove({user:"username"})
- 查看所有用戶
db.system.users.find()
- 用戶登錄
mongo 127.0.0.1:27017/username
主從復制
- 主服務器配置
master.conf
dbpath=D:\MongoDB\master
port = 8888
bind_ip = 127.0.0.1
master=true
- 從服務器配置
dbpath=D:\MongoDB\slave
port = 7777
bind_ip = 127.0.0.1
source = 127.0.0.1:8888 #主服務器資料源
slave = true
- 主服務器運行
db.getMongo().setSlaveOk();
(rs.setSlaveOk())
- 索引碎片整理
db.values.reIndex()
- 查詢分析器開啟
db.setProfilingLevel(2)
- 強制使用指定索引
hing({indexname:1})
- 通配符
$** 匹配任意欄位中包含的字串
-- 決議字串>>>洗掉分詞>>>匹配詞干
-- 關鍵字使用雙引號包含,必須匹配含有雙引號的短語
db.books.find({$text:{$search:'"books"'}}) 匹配含有books的短語
- 排除單詞
db.books.find({$text:{$search:'mongodb -second'}}) 搜索匹配mongodb但不包含second的短語
- 排除檔案
db.books.find({$text:{$search:'mongodb -"second" '}}) 搜索匹配mongodb但不包含second的檔案
- 分配權重(默認為1,其他值為被發現的概率的倍數,即對索引的影響)
db.books.createIndex(
{'$**': 'text'},<--使用字串索引索引所有欄位
{weights:
{title: 10, <---為欄位指定1以外的權重
dategories:5}
}
)
- 在索引里指定語言
db.books.createIndex(
{'$**':'text'},
{weights:
{title:10,
categories:5},
name:'index_name',
default_language:'french'
}
)
切換存盤引擎到WiredTiger
- 設定組態檔YAML
storage:
dbpath:"/data/db" 資料庫存盤路徑
joural:
enabled:true 是否啟用日志,默認true
engine:"wiredTiger" 存盤引擎,默認mmapv1
wiredTiger:
engineConfig:
cacheSizeGB:8 使用記憶體大小,默認是物理記憶體的一半,至少1G
journalCompressor:none 日志壓縮器,默認snappy, 設定為none獲取最佳性能
collectionConfig:
blockCompressor:none 集合資料壓縮器(none,snappy,zlib)
indexConfig:
prefixCompression:false 是否為索引使用壓縮
collection.aggregate(pipeline)
pipline=[{$match:?},
{$group:?},
{$sort:?},
{$limit:?},
{$skip:?},
{$unwind:?},
{$redact:?},
{$sample:?},
{$out:?}
]
-
$match:選擇24號發布的商品且交易時長為三天的商品
-
$group:打組,實際上是求和、平均等計算性的打組
-
按照price進行分組,每相同price的加1,即求每種價格出現的次數
-
$sort:對'counts'進行排序,-1代表從高到低排序,1代表順序
-
$limit:和find中limit一樣+
-
$price相當于把原始資料中price勾過來
pipeline = [
{'$match':{'$and':[{'pub_date':'2016.12.24'},{'time':3}]}},
{'$group':{'_id':'$price','counts':{'$sum':1}}},
{'$sort' :{'counts':-1}},
{'$limit':10}
]
-
'\(slice':['\)cates',2,1] 2表示跳過兩個,選擇第三個
-
'$group'更新_id 新建 counts欄位
-
按照count欄位排序
pipeline2 = [
{'$match':{'$and':[{'pub_date':'2015.12.24'},{'time':3}]}},
{'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}},
{'$sort':{'counts':-1}}
]
- 呼叫aggregate函式
item_info.aggregate(pipeline)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/37961.html
標籤:NoSQL
上一篇:Redis 復制程序詳解
