課程地址:mongoDB入門篇
幾個重要的網(wǎng)站
mongoDB的特點
- NoSql數(shù)據(jù)庫,不支持Sql語言的數(shù)據(jù)庫
- 沒有表結構的概念,每個表可以有完全不同的結構
{name:'小明', sex:'男'}
{name:'小紅', address:'上海'}
{name:'小藍', home:{'山東','江西'}}
完全的索引支持
- 單鍵索引
{x:1}
- 多鍵索引
{x:1, y:1}
- 數(shù)組索引
["apple", "lemon"]
- 全文索引
"i am a little bird. "
- 地理位置索引2D
常用命令
顯示所有的數(shù)據(jù)庫
show dbs
切換數(shù)據(jù)庫[沒有就新建數(shù)據(jù)庫]
use imooc
刪除數(shù)據(jù)庫
use imooc
db.dropDatabase()
show dbs
刪除集合
db.coll.drop()
show collections
show tables
數(shù)據(jù)庫集合中插入數(shù)據(jù)
use imooc
db.coll.insert({x:1})
# 可以插入結構完全不同的記錄
db.coll.insert({x:100, y:100, z:100})
循環(huán)插入100個值
for(var i=1; i<=100; i++){
db.coll.insert({x:i})
}
計算集合記錄的個數(shù)
db.coll.find().count()
分頁查找(顯示第4-5條)
默認排序是按照id進行排序的
# 升序
db.coll.find().skip(3).limit(2).sort({x:1})
# 降序
db.coll.find().skip(3).limit(2).sort({x:-1})
#自然排序是按照寫入的時間進行排序
db.coll.find().sort({
$natural:-1
}).limit(10)
刪除所有符合指定條件的值
db.coll.remove({x:1})
刪除嵌套文檔中
//插入一條文檔
db.profiles.insert({ votes: [ 3, 5, 6, 7, 7, 8 ] })
//移除數(shù)組中所有元素7
db.profiles.update( { votes: 3 }, { $pull: { votes: 7 } } )
//移除數(shù)組中所有大于6的元素
db.profiles.update( { votes: 3 }, { $pull: { votes: { $gt: 6 } } } )
例2
db.users.update({
userId:'001'
},{
$pull:{
cartList:{
productName:'香蕉'
}
}
})
顯示所有的集合
show collections
查詢集合中所有的記錄
db.coll.find()
查詢指定條件的記錄
db.coll.find({x:1})
db.coll.find({x:100})
插入相同id報錯
db.coll.insert({x:2, _id:1})
db.coll.insert({x:3, _id:1})
更新數(shù)據(jù)
db.coll.update({x:1}, {x:999})
db.coll.find().sort({x:-1})
#只改變y的值醋旦,其他的值不變
db.coll.update({z:100}, {$set:{y:99}})
#恢復
db.coll.update({z:100}, {$set:{y:100}})
#屬性y以外的值會被刪除掉
db.coll.update({z:100}, {y:99})
#恢復
db.coll.update({y:99}, {x:100, y:100, z:100})
嵌套數(shù)據(jù)結構的更新
db.users.insert({
userId:'001',
cartList:[
{
productName:'蘋果',
productNum:1,
checked:false
},
{
productName:'香蕉',
productNum:2,
checked:true
}
]
})
db.users.find({
cartList.productName:'蘋果'
})
db.users.update({
userId:'001',
'cartList.productName':'蘋果'
},{
$set:{
'cartList.$.productNum':10
}
})
默認只更新第一條數(shù)據(jù)
for(var i=1; i<4; i++){
db.coll.insert({c:1})
}
db.coll.find({c:1}) //3 results
db.coll.update({c:1}, {c:2}) //1 result modify
db.coll.find({c:1})//2 results
db.coll.find({c:2})//1 result
修改所有的記錄
db.coll.update({c:1}, {$set:{c:2}}, false, true)
db.coll.find({c:2})//3 result
查找的數(shù)據(jù)不存在自動創(chuàng)建一條
#不存在的數(shù)據(jù)
db.coll.find({y:101})
#更新不存在的數(shù)據(jù)
db.coll.update({y:101}, {y:999})
#沒有數(shù)據(jù)被修改
db.coll.find({y:999})
#添加參數(shù)true
db.coll.update({y:101}, {y:999}, true)
#添加了一條數(shù)據(jù)
db.coll.find({y:999})
索引
- _id索引(默認建立的索引)
- 單鍵索引
- 多鍵索引
- 復合索引
- 查詢條件不只有一個時,就需要建立復合索引
- 插入{x:1, y:2, z:3}記錄
- 按照x與y的值查詢
- 創(chuàng)建索引db.collection.ensureIndex({x1, y:1})
- 使用{x:1, y:1}作為條件進行查詢
- 過期索引
- 是在一段時間后會過期的索引
- 在索引過期后,相應的數(shù)據(jù)會被刪除
- 適合存儲一些在一段時間后會失效的數(shù)據(jù)期奔,比如用戶的登錄信息柱搜、存儲的日志
- 創(chuàng)建方法:db.coll.ensureIndex({time:1}, {expireAfterSeconds:10})
- 數(shù)據(jù)類型必須是ISODate或者ISODate數(shù)組,不能使用時間戳,否則不能被自動刪除
- 指定了ISODate數(shù)組述呐,則按照最小的時間進行刪除
- 不能是符合索引
- 刪除時間不是精確
- 刪除過程是由后臺程序每60s跑一次寺惫,而且刪除也需要一些時間疹吃,所以存在誤差
db.coll.ensureIndex({time:1}, {expireAfterSeconds:10})
db.coll.insert({time:new Date()})
db.coll.find()
- 全文索引
- 對字符串與字符串數(shù)組創(chuàng)建全文可搜索的索引
- 適用情況:{author:"", title:"", article:""}
- 建立方法:
- db.coll.ensureIndex({"key_1":"text"})
- db.coll.ensureIndex({"key_1":"text", key_2:"text"})
- db.coll.ensureIndex({"$**":"text"})(集合中所有字段建立索引)
- 全文索引查詢
- db.coll.find({$text:{$search:"rr"}})
- db.coll.find({$text:{$search:"aa bb cc"}}) (包含aa或bb或cc)
- db.coll.find({$text:{$search:""aa" "bb" "cc""}}) (引號表示既包括aa又包括bb,cc是一種并的關系)
- db.coll.find({$text:{$search:"aa bb -cc"}}) (不包括cc)
- db.coll.find({$text:{$search:""aa" bb cc"}})
- 全文索引相似度
- $meta操作符:{score:{$meta:"textScore"}}
- 寫在查詢條件后面可以返回結果的相似度
- 與sort一起使用,可以達到很好的實用效果
- db.coll.insert({"article":"aa bb"})
- db.coll.find({$text:{$search:"aa bb"}}, {score:{$meta:"textScore"}})
- db.coll.find({$text:{$search:"aa bb"}}, {score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})
- 限制
- 每次查詢西雀,只能指定一個$text查詢
- $text查詢不能出現(xiàn)在$nor查詢中
- 查詢中如果包含了$text,hint不再起作用
- 全文索引不支持中文
db.coll.ensureIndex({"article":"text"})
db.coll.insert({"article":"aa bb cc dd ee"})
db.coll.insert({"article":"aa bb rr gg"})
db.coll.insert({"article":"aa bb cc hh dojiofjofjqjfq"})
db.coll.find({$text:{$search:"rr"}})
db.coll.find({$text:{$search:"aa bb cc"}})
db.coll.find({$text:{$search:"\"aa\" \"bb\" \"cc\""}})
db.coll.find({$text:{$search:"aa bb -cc"}})
db.coll.find({$text:{$search:"\"aa\" bb cc"}})
- 地理位置索引
索引操作
db.coll.getIndexes()
db.coll.ensureIndex({c:1})
索引屬性
創(chuàng)建格式
db.coll.ensureIndex({param}, {param})
第二個參數(shù)便是索引的屬性
指定索引名字
db.coll.ensureIndex({x:1,y:1,m:1}, {name:"normal_index"})
db.coll.getIndexes()
刪除指定的索引
db.coll.dropIndex("normal_index")
db.coll.getIndexes()
索引的唯一性
db.coll.ensureIndex({m:1, n:1},{unique:true})
db.coll.insert({m:1, n:2})
db.coll.insert({m:1, n:2})
這樣報錯萨驶,索引值相同了
稀疏性,sparse指定
對不存在的字段不創(chuàng)建索引艇肴,這就是稀疏性
db.coll.insert({m:1})
db.coll.insert({n:1})
只查找存在m字段的記錄
db.coll.find({m:{$exists:true}})
建立稀疏索引
db.coll.ensureIndex({m:1},{spare:true})
有稀疏索引腔呜,false處于監(jiān)控之外
db.coll.find({m:{$exists:false}})
強制使用索引(新版本似乎解決了這個問題)
db.coll.find({m:{$exists:false}}).hint("m_1")
地理位置索引
- 概念:將一些點的位置存儲在MongoDB中,創(chuàng)建索引后再悼,可以按照位置來查找其他點
- 2d索引
[平面地理位置索引]
育谬,用于存儲和查找平面上的點 - 位置表示方式:經(jīng)緯度[經(jīng)度, 維度]
- 取值范圍:經(jīng)度[-180,180] 維度[-90, 90]
- 2dsphere索引
[球面地理位置索引]
帮哈,用戶存儲和查找球面上的點
- 2d索引
- 查找方式
- 查找距離某個點一定距離內(nèi)的點
- 查找包含在某區(qū)域內(nèi)的點
- 創(chuàng)建方式
db.location.ensureIndex({w:"2d"})
db.location.insert({w:[1, 1]})
db.location.insert({w:[1, 2]})
db.location.insert({w:[3, 2]})
db.location.insert({w:[100, 100]})
db.location.insert({w:[180, 80]})
db.location.insert({w:[200, 100]})
最后報錯膛檀,超出了指定的范圍
- 查詢方式
- $near查詢:查詢距離某個點最近的點
db.location.find({w:{$near:[1,1]}})
db.location.find({w:{$near:[1,1],$maxDistance:10}})
* $geoWithin查詢:查詢某個形狀內(nèi)的點
db.location.find({
w:{
$geoWithin:{
$box:[[0,0], [3,3]]
}
}
})
db.location.find({
w:{
$geoWithin:{
$box:[[1,1], [2,3]]
}
}
})
db.location.find({
w:{
$geoWithin:{
$center:[[0, 0], 5]
}
}
})
db.location.find({
w:{
$geoWithin:{
$polygon:[[0, 0], [0,1], [2,5], [6,1]]
}
}
})
2d索引
形狀的表示:
- $box:矩形使用 {$box:[[x1, y1], [x2,y2]]}
- $center圓形使用 {$center:[[x1, y1], r]}
- $polygon:多邊形使用 {$polygon:[[x1,y1], [x2,y2], [x3,y3]]}
runCommand命令
db.runCommand({
geoNear:"location",
near:[1,2],
//minDistance:10,(對2d索引無效)
maxDistance:10,
num:1
})
2dsphere索引
- 概念:球面地理位置索引
db.collection.ensureIndex({
w:"2dsphere"
})
- 位置表示
- GeoJSON:描述一個點,一條直線娘侍,多邊形等形狀
- 格式
- {type:"", coordinates:[coordinates]}
創(chuàng)建用戶
- 內(nèi)置角色類型
- read, readWrite, dbAdmin, dbOwner, userAdmin
- 創(chuàng)建用戶
db.createUser({
user:"admin",
pwd:"123456",
roles:[
{
role:"userAdmin",
db:"imooc"
}
]
})
退出重進需要密碼[現(xiàn)在不需要密碼]