數(shù)據(jù)庫基本操作
網(wǎng)易公開課-MongoDB數(shù)據(jù)庫學(xué)習(xí)筆記
# use study胞谈,并不會(huì)創(chuàng)建數(shù)據(jù)庫吏奸,只有在數(shù)據(jù)保存之后才會(huì)創(chuàng)建
>use study
# 創(chuàng)建集合
>db.createCollection("emp")
# 此時(shí)再次查看數(shù)據(jù)庫酝陈,才會(huì)看到study已經(jīng)創(chuàng)建
>show dbs
# 實(shí)際在使用的過程中豺型,不會(huì)特意先創(chuàng)建集合,而是直接在集合中插入數(shù)據(jù)饼齿,mongodb會(huì)自動(dòng)創(chuàng)建
>db.dept.insert({"deptno": 10, "dname": "財(cái)務(wù)部", "loc": "北京"})
# 此時(shí)MongoDB會(huì)自動(dòng)創(chuàng)建一個(gè)dept的集合饲漾,并插入一條數(shù)據(jù)
# MongoDB數(shù)據(jù)庫的集合結(jié)構(gòu)很靈活,無模式集合缕溉。
# 查看集合內(nèi)容
# 語法: db.集合名詞.find({若干條件})
>db.dept.find()
{ "_id" : ObjectId("586a562f87e00ea77b041b67"), "deptno" : 10, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
# ID "時(shí)間戳 + 機(jī)器碼 + PID + 計(jì)數(shù)器"考传,肯定不會(huì)重復(fù)
# 查找某個(gè)具體實(shí)例
>db.dept.find({"_id" : ObjectId("586a57b287e00ea77b041b69")})
>db.dept.findOne()
# 刪除數(shù)據(jù)
# db.集合名詞.remove({條件})
>db.dept.remove({"_id" : ObjectId("586a57b287e00ea77b041b69")})
# 更新數(shù)據(jù)
# db.集合名詞.update({條件}, {更新后的數(shù)據(jù)})
>db.dept.update({"_id" : ObjectId("586a562f87e00ea77b041b67")}, {"loc": "上海"})
# 刪除集合
# db.集合名詞.drop()
db.dept.drop()
# 刪除數(shù)據(jù)庫(刪除當(dāng)前所在的數(shù)據(jù)庫)
db.dropDatabase()
數(shù)據(jù)插入
# 增加單條數(shù)據(jù)
# db.集合.insert({數(shù)據(jù)json})
# 增加多條數(shù)據(jù),數(shù)組形式
# db.集合.insert([{},{}])
# 批量數(shù)據(jù)javascript語法
for (var x = 0; x < 100; x ++) {
db.infos.insert({"url": "xxx" + x})
}
數(shù)據(jù)查詢
# db.集合名詞.find({查詢條件},{顯示字段})
# {查詢條件}证鸥,json格式
>db.infos.find({"url": "wwww.baidu.com"})
#{顯示字段}:數(shù)據(jù)的投影操作僚楞,需要顯示的字段設(shè)置為1,不顯示的設(shè)置為0
>db.infos.find({},{"_id": 0})
# pretty顯示枉层,格式化的輸出效果
db.infos.find({},{"_id": 0}).pretty()
數(shù)據(jù)集
# 測試數(shù)據(jù)集
>db.students.drop()
>db.students.insert({"name": "張三", "sex": "男", "age": 19, "score": 89, "address": "海淀區(qū)"})
>db.students.insert({"name": "李四", "sex": "女", "age": 20, "score": 59, "address": "朝陽區(qū)"})
>db.students.insert({"name": "王五", "sex": "女", "age": 19, "score": 99, "address": "西城區(qū)"})
>db.students.insert({"name": "趙六", "sex": "男", "age": 20, "score": 100, "address": "東城區(qū)"})
>db.students.insert({"name": "孫七", "sex": "男", "age": 19, "score": 20, "address": "海淀區(qū)"})
>db.students.insert({"name": "王八", "sex": "女", "age": 21, "score": 0, "address": "海淀區(qū)"})
>db.students.insert({"name": "劉九", "sex": "男", "age": 19, "score": 70, "address": "朝陽區(qū)"})
>db.students.insert({"name": "錢十", "sex": "女", "age": 21, "score": 56, "address": "西城區(qū)"})
>db.students.insert({"name": "數(shù)組-A", "sex": "女", "age": 21, "score": 56, "address": "西城區(qū)", "course": ["語文", "數(shù)學(xué)", "英語", "音樂", "政治"]})
>db.students.insert({"name": "數(shù)組-B", "sex": "女", "age": 21, "score": 56, "address": "西城區(qū)", "course": ["語文", "數(shù)學(xué)", "英語"]})
>db.students.insert({"name": "數(shù)組-C", "sex": "女", "age": 21, "score": 56, "address": "西城區(qū)", "course": ["語文", "政治"]})
>db.students.insert({"name": "數(shù)組-D", "sex": "女", "age": 21, "score": 56, "address": "西城區(qū)", "course": ["英語", "音樂", "政治"]})
>db.students.insert({"name": "嵌套-A", "sex": "女", "age": 21, "score": 56, "address": "西城區(qū)", "course": ["英語", "音樂", "政治"], "parents": [{"name": "父親A", "job": "工人"}, {"name": "母親A", "job": "員工"}]})
>db.students.insert({"name": "嵌套-B", "sex": "女", "age": 21, "score": 56, "address": "西城區(qū)", "course": ["英語", "音樂", "政治"], "parents": [{"name": "父親B", "job": "局長"}, {"name": "母親B", "job": "職員"}]})
關(guān)系運(yùn)算
# 支持的關(guān)系操作 等于泉褐,大于($gt), 小于($lt)鸟蜡,大于等于($gte)膜赃,小于等于($lte),不等于($ne)
# 等于查詢
>db.students.find({"name": "張三"}).pretty()
{
"_id" : ObjectId("586bbc0885913b9e29437679"),
"name" : "張三",
"sex" : "男",
"age" : 19,
"score" : 89,
"address" : "海淀區(qū)"
}
# 大于
>db.students.find({"age": {"$gt": 19}}).pretty()
# 大于等于
>db.students.find({"score": {"$gte": 60}}).pretty()
# 不等于
>db.students.find({"name": {"$ne":"張三"}}).pretty()
邏輯運(yùn)算
# 支持的邏輯運(yùn)算 與($and) 或($or) 非($nor)
# 與操作 and連接 查詢19~20歲的學(xué)生
>db.students.find({"age": {"$gte": 19, "$lte": 20}}).pretty()
# 或 or
>db.students.find({"$or": [{"age": {"$gt": 19}}, {"score": {"$gt": 90}}]}).pretty()
# 非
>db.students.find({"$nor": [{"age": {"$gt": 19}}, {"score": {"$gt": 90}}]}).pretty()
求模運(yùn)算
> db.students.find({"age": {"$mod" : [20, 1]}}).pretty()
{
"_id" : ObjectId("586bbc0885913b9e2943767e"),
"name" : "王八",
"sex" : "女",
"age" : 21,
"score" : 0,
"address" : "海淀區(qū)"
}
{
"_id" : ObjectId("586bbc0985913b9e29437680"),
"name" : "錢十",
"sex" : "女",
"age" : 21,
"score" : 56,
"address" : "西城區(qū)"
}
范圍查詢
# $in , $nin
>db.students.find({"name": {"$in": ["張三", "李四","王五"]}}).pretty()
>db.students.find({"name": {"$nin": ["張三", "李四","王五"]}}).pretty()
數(shù)組運(yùn)算
# $all, $size, $slice, $elemMatch
# 所有學(xué)生信息里面揉忘,包含語文财剖,數(shù)學(xué)
>db.students.find({"course": {"$all": ["語文", "數(shù)學(xué)"]}}).pretty()
>db.students.find({"address": {"$all": ["海淀區(qū)"]}}).pretty()
# 數(shù)組下標(biāo)查詢
>db.students.find({"course.1": "數(shù)學(xué)"}).pretty()
#只參加兩門課程
>db.students.find({"course": {"$size": 2}}).pretty()
# 只顯示前兩門
>db.students.find({"age": 21}, {"course": {"$slice": 2}}).pretty()
>db.students.find({"age": 21}, {"course": {"$slice": -2}}).pretty()
# $slice: [start_index, length]
>db.students.find({"age": 21}, {"course": {"$slice": [1, 2]}}).pretty()
# 嵌套符合
>db.students.find({"$and": [{"age": {"$gt": 20}}, {"parents": {"$elemMatch": {"job": "局長"}}}]}).pretty()
是否存在
# exists, 數(shù)據(jù)過濾
> db.students.find({"parents": {"$exists": true}}).pretty()
條件過濾
# 能利用JavaScript,但是不方便使用索引
>db.students.find({"$where": "this.age > 20"}).pretty()
>db.students.find("this.age > 20").pretty()
>db.students.find(function() {return this.age > 20 && this.age <= 21}).pretty()
正則運(yùn)算
#模糊查詢癌淮,必須使用正則表達(dá)式
#Perl兼容的正則表達(dá)式
#{key: 正則標(biāo)記}
# {key: {"$regex": 正則標(biāo)記, "$options": 選項(xiàng)}},
# options: "i": 忽略字母大小寫 "m" 多行查找 "x": 空白字符串除了被轉(zhuǎn)義的或在字符串中意外的完全被忽略 "s": 匹配所有字符(圓點(diǎn).)沦补,包括換行內(nèi)容
>db.students.find({"name": /數(shù)組/}).pretty()
>db.students.find({"name": /a/i}).pretty() # 不區(qū)分大小寫
>db.students.find({"name": {"$regex": /a/i}}).pretty()
>db.students.find({"course": /語/}).pretty()
數(shù)據(jù)排序
# sort()函數(shù) 升序(1) 降序(-1)
>db.students.find().sort({"score": 1}).pretty()
>db.students.find().sort({"score": -1}).pretty()
# nature 自然排序
>db.students.find().sort({"$nature": 1}).pretty()
數(shù)據(jù)分頁
# skip(n)跨過多少數(shù)據(jù)
# limit(n)取多少數(shù)據(jù)
>db.students.find().skip(2).limit(5).pretty()
數(shù)據(jù)更新操作
# mongodb更新非常麻煩乳蓄,最好的做法是:先刪后重新插入
數(shù)據(jù)刪除
# remove()函數(shù)
# db.集合.remove()
>db.students.remove({})
>db.students.remove({"name": /數(shù)組/})
>db.students.remove({"name": /數(shù)組/}, 1) # 只刪除符合條件的1個(gè)
>db.students.drop() # 刪除結(jié)婚
游標(biāo)
# hasNext() next()
>var cursor = db.students.find()
>cursor.hasNext()
>cursor.next()
while(cursor.hasNext()) {
var doc = cursor.next()
printjson(doc)
}