## 看看mongodb的基本操做mongodb
### mongodb的基本使用包括幾個方面shell
增、刪、查、改是重點
數據庫
CRUDjson
C:insert()數組
R:find()ide
U:update()ui
D:remove()url
### 增長數據spa
[root@blog ~]# hostname blog.unix178.com [root@blog ~]# mongo MongoDB shell version: 2.4.6 connecting to: test > show dbs admin0.203125GB local0.078125GB test0.203125GB > use zhuima switched to db zhuima > info = {Name:"zhuima",Age:26,Gender:"F",Address:"Beijing China",Work:"Engineer",Other:"DevOps"} { "Name" : "zhuima", "Age" : 26, "Gender" : "F", "Address" : "Beijing China", "Work" : "Engineer", "Other" : "DevOps" } > db.peroson.insert(info) > db.person.find() > db.peroson.find() { "_id" : ObjectId("53cbc52255ea1fd13e47ad9d"), "Name" : "zhuima", "Age" : 26, "Gender" : "F", "Address" : "Beijing China", "Work" : "Engineer", "Other" : "DevOps" } >
### 一樣咱們能夠批量插入數據命令行
> for(x=1;x<=1000;x++) db.peroson.insert({Name:"zhuima"+x,Age:x%100,Address:x+".Number Beijing China"}) > db.peroson.find() { "_id" : ObjectId("53cbc52255ea1fd13e47ad9d"), "Name" : "zhuima", "Age" : 26, "Gender" : "F", "Address" : "Beijing China", "Work" : "Engineer", "Other" : "DevOps" } { "_id" : ObjectId("53cbc67855ea1fd13e47ad9e"), "Name" : "zhuima1", "Age" : 1, "Address" : "1.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ad9f"), "Name" : "zhuima2", "Age" : 2, "Address" : "2.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada0"), "Name" : "zhuima3", "Age" : 3, "Address" : "3.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada1"), "Name" : "zhuima4", "Age" : 4, "Address" : "4.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada2"), "Name" : "zhuima5", "Age" : 5, "Address" : "5.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada3"), "Name" : "zhuima6", "Age" : 6, "Address" : "6.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada4"), "Name" : "zhuima7", "Age" : 7, "Address" : "7.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada5"), "Name" : "zhuima8", "Age" : 8, "Address" : "8.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada6"), "Name" : "zhuima9", "Age" : 9, "Address" : "9.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada7"), "Name" : "zhuima10", "Age" : 10, "Address" : "10.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada8"), "Name" : "zhuima11", "Age" : 11, "Address" : "11.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada9"), "Name" : "zhuima12", "Age" : 12, "Address" : "12.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaa"), "Name" : "zhuima13", "Age" : 13, "Address" : "13.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adab"), "Name" : "zhuima14", "Age" : 14, "Address" : "14.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adac"), "Name" : "zhuima15", "Age" : 15, "Address" : "15.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adad"), "Name" : "zhuima16", "Age" : 16, "Address" : "16.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adae"), "Name" : "zhuima17", "Age" : 17, "Address" : "17.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaf"), "Name" : "zhuima18", "Age" : 18, "Address" : "18.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb0"), "Name" : "zhuima19", "Age" : 19, "Address" : "19.Number Beijing China" } Type "it" for more >
### 刪除數據
> db.peroson.remove({Name:"zhuima"}) > db.peroson.find() { "_id" : ObjectId("53cbc67855ea1fd13e47ad9e"), "Name" : "zhuima1", "Age" : 1, "Address" : "1.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ad9f"), "Name" : "zhuima2", "Age" : 2, "Address" : "2.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada0"), "Name" : "zhuima3", "Age" : 3, "Address" : "3.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada1"), "Name" : "zhuima4", "Age" : 4, "Address" : "4.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada2"), "Name" : "zhuima5", "Age" : 5, "Address" : "5.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada3"), "Name" : "zhuima6", "Age" : 6, "Address" : "6.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada4"), "Name" : "zhuima7", "Age" : 7, "Address" : "7.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada5"), "Name" : "zhuima8", "Age" : 8, "Address" : "8.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada6"), "Name" : "zhuima9", "Age" : 9, "Address" : "9.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada7"), "Name" : "zhuima10", "Age" : 10, "Address" : "10.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada8"), "Name" : "zhuima11", "Age" : 11, "Address" : "11.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada9"), "Name" : "zhuima12", "Age" : 12, "Address" : "12.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaa"), "Name" : "zhuima13", "Age" : 13, "Address" : "13.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adab"), "Name" : "zhuima14", "Age" : 14, "Address" : "14.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adac"), "Name" : "zhuima15", "Age" : 15, "Address" : "15.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adad"), "Name" : "zhuima16", "Age" : 16, "Address" : "16.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adae"), "Name" : "zhuima17", "Age" : 17, "Address" : "17.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaf"), "Name" : "zhuima18", "Age" : 18, "Address" : "18.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb0"), "Name" : "zhuima19", "Age" : 19, "Address" : "19.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb1"), "Name" : "zhuima20", "Age" : 20, "Address" : "20.Number Beijing China" } Type "it" for more >
### 批量刪除數據
> db.peroson.remove({Age:{$gt:1,$lt:10}}) > db.peroson.find() { "_id" : ObjectId("53cbc67855ea1fd13e47ad9e"), "Name" : "zhuima1", "Age" : 1, "Address" : "1.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada7"), "Name" : "zhuima10", "Age" : 10, "Address" : "10.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada8"), "Name" : "zhuima11", "Age" : 11, "Address" : "11.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada9"), "Name" : "zhuima12", "Age" : 12, "Address" : "12.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaa"), "Name" : "zhuima13", "Age" : 13, "Address" : "13.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adab"), "Name" : "zhuima14", "Age" : 14, "Address" : "14.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adac"), "Name" : "zhuima15", "Age" : 15, "Address" : "15.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adad"), "Name" : "zhuima16", "Age" : 16, "Address" : "16.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adae"), "Name" : "zhuima17", "Age" : 17, "Address" : "17.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaf"), "Name" : "zhuima18", "Age" : 18, "Address" : "18.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb0"), "Name" : "zhuima19", "Age" : 19, "Address" : "19.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb1"), "Name" : "zhuima20", "Age" : 20, "Address" : "20.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb2"), "Name" : "zhuima21", "Age" : 21, "Address" : "21.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb3"), "Name" : "zhuima22", "Age" : 22, "Address" : "22.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb4"), "Name" : "zhuima23", "Age" : 23, "Address" : "23.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb5"), "Name" : "zhuima24", "Age" : 24, "Address" : "24.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb6"), "Name" : "zhuima25", "Age" : 25, "Address" : "25.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb7"), "Name" : "zhuima26", "Age" : 26, "Address" : "26.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb8"), "Name" : "zhuima27", "Age" : 27, "Address" : "27.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb9"), "Name" : "zhuima28", "Age" : 28, "Address" : "28.Number Beijing China" } Type "it" for more >
### 指定某個條件查找數據
> db.peroson.findOne() { "_id" : ObjectId("53cbc67855ea1fd13e47ad9e"), "Name" : "zhuima1", "Age" : 1, "Address" : "1.Number Beijing China" } > db.peroson.find({Name:"zhuima888"}) { "_id" : ObjectId("53cbc67855ea1fd13e47b115"), "Name" : "zhuima888", "Age" : 88, "Address" : "888.Number Beijing China" } >
### 指定多重條件查找
> db.peroson.find({Age:{$gt:33,$lt:44}}) { "_id" : ObjectId("53cbc67855ea1fd13e47adbf"), "Name" : "zhuima34", "Age" : 34, "Address" : "34.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc0"), "Name" : "zhuima35", "Age" : 35, "Address" : "35.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc1"), "Name" : "zhuima36", "Age" : 36, "Address" : "36.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc2"), "Name" : "zhuima37", "Age" : 37, "Address" : "37.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc3"), "Name" : "zhuima38", "Age" : 38, "Address" : "38.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc4"), "Name" : "zhuima39", "Age" : 39, "Address" : "39.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc5"), "Name" : "zhuima40", "Age" : 40, "Address" : "40.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc6"), "Name" : "zhuima41", "Age" : 41, "Address" : "41.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc7"), "Name" : "zhuima42", "Age" : 42, "Address" : "42.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc8"), "Name" : "zhuima43", "Age" : 43, "Address" : "43.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae23"), "Name" : "zhuima134", "Age" : 34, "Address" : "134.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae24"), "Name" : "zhuima135", "Age" : 35, "Address" : "135.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae25"), "Name" : "zhuima136", "Age" : 36, "Address" : "136.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae26"), "Name" : "zhuima137", "Age" : 37, "Address" : "137.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae27"), "Name" : "zhuima138", "Age" : 38, "Address" : "138.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae28"), "Name" : "zhuima139", "Age" : 39, "Address" : "139.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae29"), "Name" : "zhuima140", "Age" : 40, "Address" : "140.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae2a"), "Name" : "zhuima141", "Age" : 41, "Address" : "141.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae2b"), "Name" : "zhuima142", "Age" : 42, "Address" : "142.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae2c"), "Name" : "zhuima143", "Age" : 43, "Address" : "143.Number Beijing China" } Type "it" for more >
### 更新數據
> db.peroson.find({Name:"zhuima100"}) { "_id" : ObjectId("53cbc67855ea1fd13e47ae01"), "Name" : "zhuima100", "Age" : 0, "Address" : "100.Number Beijing China" } > db.peroson.find({Name:"zhuima100"}) { "_id" : ObjectId("53cbc67855ea1fd13e47ae01"), "Name" : "zhuima100", "Age" : 100, "Address" : "100.Number Beijing China" } >
### 批量更新數據
數組修改器
$push 會向已有的數組末尾加入一個元素,要是沒有就會建立一個新的數組
> db.person.findOne() { "_id" : ObjectId("53615d0b447315f76c35aee9"), "user_id" : "zhuima", "age" : 27, "tel" : 12335345, "add" : "beijing.tiananmen" } > db.person.update({"tel":12335345},{"$push":{"info":{"gender":"male","email":"xxx@xxx.com"}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.person.findOne({"tel":12335345}) { "_id" : ObjectId("53615d0b447315f76c35aee9"), "user_id" : "zhuima", "age" : 27, "tel" : 12335345, "add" : "beijing.tiananmen", "info" : [ { "gender" : "male", "email" : "xxx@xxx.com" } ] } >
## 看了上面的一些例子,咱們來點短小的命令
### 命令行接口
mongo >
### 查看全部數據庫
> show dbs
### 查看數據庫中全部的表
後面的;無關緊要,看我的喜愛
> show dbs; > use zhuima; > show collections;
### 爲數據庫下某個表插入數據
> j = { name : "zhuima"} > k = { age : 23} > m = {sex : "fmale"} > db.test.insert(j) > db.test.insert(k) > db.test.insert(m) >
### 查看某個數據庫中的數據
> db.test.find() # 默認顯示前20行 能夠再>後接着輸入it顯示下一頁的20行數據~ >
### 查看數據庫中的某個表的數據第一行
> db.test.findOne() >
### 查看數據庫中的某個表的前幾個
limit限制輸出信息量
> db.test.find().limit(3) 只列出前幾行 >
### 如何顯示數據庫中全部信息(突破只能顯示20行的限制)
> var c = db.test.find() > while (c.hasNext()) printjson(c.next()) >
### 對數據進行篩選
> db.text.find({x:3}) #篩選出x對應值爲3的匹配的行 >
### 批量插入數據
> for (i =1; i<=25;i++) db.test.insert({x:i}) >
### 只列出從第幾行開始的行(有規律的)
sort進行排序操做
$gt是mongodb內置的比較運算的內置變量
> db.test.find({x:{$gt:5}}).sort({x:3}) #列出從第6行開始的行並進行排序 >
### 只列出某個範圍內的行
> db.test.find({x:{$gt:5}}).limit(5) # 只列出第6行到第10行 >
### 過濾包含某些關鍵字的篩選
$in也是mongodb內置的比較運算的內置變量
> db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } ) > db.zhuima.find({name:{$in:["zhuima"]}}) { "_id" : ObjectId("53b272e9c74fabe96af3a054"), "name" : "zhuima" } >
### 數組定義
> i={"date":new Date()}
### 能夠嵌套數組的
> name={"name":{"gender":"male"}} { "name" : { "gender" : "male" } } >
### 定義一個名爲追馬的人的相關信息
> info={"name":"zhuima","other":["male","single"],"age":22} { "name" : "zhuima", "other" : [ "male", "single" ], "age" : 22 } >
內置變量(非徹底)
### 元素查詢
$exists 檢查是否存在
db.collections.find({address:{$exists:true}}) 驗證存在address這個字符串的數據
$mod 將指定字段的值進行取模運算
$type 返回指定字段的值類型
### 邏輯運算
$or
$and
$not
$nor
db.collections.find({$and :{age :{$gte :61}},{age :{$lte 80}}}})
將大於61小於80的打印出來
### 比較運算
$gt
$gte
$in
$lt
$lte
$ne
$nin
### 只打印指定信息
{age:1},{name:1} 1表示爲真
### 更新文檔
db.collections.update({age:{$gt:80}},{$set:{Gender:"F"}})
將年齡大於80的性別所有更改成男
### Field類經常使用的操做
$inc 增長已有鍵的值
$rename 重命名已有鍵
$set 在原來的基礎上進行數據的增長,若是存在則添加,若是不存在就新建
$unset 刪除某個字段