標籤:utf-8 說明 dmi point 關聯式資料庫 字元 第一個 mongodb nosql
MongoDB是一個基於 分布式 檔案儲存體的開來源資料庫系統
在高負載的情況下,添加更多的節點,可以保證伺服器效能
MongoDB將資料存放區為一個文檔,資料結構由索引值(key=value)對組成.
1.1(Linux)MongoDB將資料目錄存在data目錄的db目錄下,需要我們自己主動建立。
mkdir -p /data/db (/datat/db是MongoDB預設的啟動的資料庫路徑 --dpath)
1.2 MongoDB後台管理shell
1.3查看當前操作的文檔(資料庫):db
1.4插入簡單的記錄,並尋找它:
>db.runoob.insert({x:10}) //將數字10插入到runoob集合x欄位中
>db.runoob.find()
2.1MongoDB概念
2.2 資料庫
一個mongodb可以建立多個資料庫。
預設資料庫為”db“,儲存在data目錄下
MongoDB 中預設的資料庫為 test,如果你沒有建立新的資料庫,集合將存放在 test 資料庫中
1)show dbs可以顯示所有資料的列表
2)use 可以串連到一個指定的資料庫
>use local
switched to local
>db
local
>
2.3 文檔
文檔是一組索引值(key-value)
eg: {"site":"www.bird.com","name":"bird"}
註:1、文檔中的鍵/值是有序的。
2、文檔中的 值 不僅可以是雙引號裡面的字串,還可以是其他幾種資料類型
3、MongoDB區分類型和大小寫
4、MongoDB的文檔不能有重複的鍵
5、文檔的鍵是字串。除了少數例外情況,鍵可以使用任意UTF-8字元
2.4 集合
集合就是MongoDB文檔組,類似於RDBMS(關聯式資料庫管理系統)中的表格
2.5MongoDB資料類型
3.1 刪除資料庫
文法:db.dropDatabase() //刪除當前資料庫,預設為test
執行個體:
1、進入一個資料庫
>use test
switched to db test
>
2、執行刪除命令
>db.dropDatabase()
{"dropped" : "test" , "ok" : 1}
3.2 刪除集合
文法:db.collection.drop()
eg:>use test
switched to db test
>show tables
site
>db.site.drop
true
>show tables
>
3.3 刪除資料
1. 刪除"ban_friends_id":"BAN121113"資料
1 > db.test_ttlsa_com.remove({"ban_friends_id":"BAN121113"})
3.4刪除表中所有的資料
db.表名.remove({})
4.1插入文檔
文法:db.Collection_name.insert(doucument)
1)eg:在runoob資料庫中的col集合中存入文檔
>db.runoob.insert({title:‘MongoDB‘,
description:‘MongoDB是一個nosql資料庫‘,
tags:[‘mongodb‘,‘database‘,‘nosql‘]
likes:100
})
如果col集合不在資料庫裡,MongoDB會自動建立此集合
2)查看插入的文檔
>db.runoob.find()
4.2 將資料定義為一個變數
>document=({title=‘MongoDB‘,
description:‘MongoDB是一個Nosql資料庫‘,
tags:[‘mongodb‘,‘database‘,‘nosql‘]
likes:100
});
執行插入操作:
>db.col.insert(document)
WriteResult({"nInserted":1})
>
也可以使用db.clo.save(document)
5、查詢
5.1.1小於 pretty() 方法以格式化的方式來顯示所有文檔
eg:db.access_points.find({"admin_id":{$lt:50}}).pretty()
5.1.2小於或等於
eg:db.access_points.find({"admin_id":{$lte:50}}).pretty()
5.1.3大於:
eg:db.access_points.find({"admin_id":{$gt:50}})
5.1.4大於或等於
eg:db.access_points.find({“admin_id”:{$gle:50}})
5.1.5 不等於
eg:db.access_points.find({"admin_id":{$ne:50}})
5.2 去重查詢
eg: db.access_controllers.distinct("admin_id")
5.3 or 或查詢
文法:
db.status.find({$ or : [{key1,value},{key2:value}]})
eg: db.access_points({$or:[{“admin_id”:1},{"mac":"111111111111"}]}).pretty()
5.4 Skip() :跳過記錄條數:
eg:db.access_points.find().skip(2) //跳過第2條資料
5.5 sort() 排序
eg:db.access_controllers.find({"type":1},{admin_id:1}).sort({admin_id:-1}) // -1降序;1 升序
5.6 db.col.find({},{"title":1,_id:0}).limit(2)
補充說明:
第一個 {} 放 where 條件,為空白表示返回集合中所有文檔。
第二個 {} 指定那些列顯示和不顯示 (0表示不顯示 1表示顯示)。
5.6 正則查詢
1)查詢包含字串test的內容
>db.posts.find({post_text:{$regex:"test"}})
2)查詢包含字元test的內容
>db.posts.find({post_text:/test/})
3) 查詢日期包含2017-09-06T01:33的字串
db.summary.find({"insert_time" : {"$gte":ISODate(/.*2017-09-06T01:33.*/)},"org_id":1},{"device":1,"insert_time":1})
MongoDB簡單操作