回顧mongodb码泞。
- 啟動
控制臺輸入mongodb吱涉,返回如下內(nèi)容
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] MongoDB starting : pid=18348 port=27017 dbpath=C:\data\db\ 64-bit host=GCX-HanChengQiang
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] db version v3.4.4
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips 22 Sep 2016
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] allocator: tcmalloc
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] modules: none
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] build environment:
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] distarch: x86_64
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] target_arch: x86_64
2018-01-16T13:58:32.680+0800 I CONTROL [initandlisten] options: {}
2018-01-16T13:58:32.681+0800 W - [initandlisten] Detected unclean shutdown - C:\data\db\mongod.lock is not empty.
2018-01-16T13:58:32.681+0800 I - [initandlisten] Detected data files in C:\data\db\ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-01-16T13:58:32.681+0800 W STORAGE [initandlisten] Recovering data from the last clean checkpoint.
2018-01-16T13:58:32.681+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3538M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2018-01-16T13:58:33.237+0800 I CONTROL [initandlisten]
2018-01-16T13:58:33.237+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-01-16T13:58:33.237+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-01-16T13:58:33.237+0800 I CONTROL [initandlisten]
2018-01-16T13:58:33.382+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/data/db/diagnostic.data'
2018-01-16T13:58:33.383+0800 I NETWORK [thread1] waiting for connections on port 27017
2018-01-16T13:58:33.688+0800 I NETWORK [thread1] connection accepted from 127.0.0.1:56218 #1 (1 connection now open)
2018-01-16T13:58:33.688+0800 I NETWORK [thread1] connection accepted from 127.0.0.1:56219 #2 (2 connections now open)
2018-01-16T13:58:33.690+0800 I NETWORK [thread1] connection accepted from 127.0.0.1:56220 #3 (3 connections now open)
2018-01-16T13:58:34.004+0800 I FTDC [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
2.新開窗口逛薇,輸入mongo捺疼,數(shù)據(jù)庫連接成功
MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.4
3.數(shù)據(jù)庫操作
use -數(shù)據(jù)庫名 //創(chuàng)建/切換數(shù)據(jù)庫
show-dbs //查看全部數(shù)據(jù)庫列表
db //顯示當前數(shù)據(jù)庫 默認是test
db.dropDatabase() //先當前數(shù)據(jù)庫下執(zhí)行,刪除當前數(shù)據(jù)庫
db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean>, size: <number>, max <number>})
//name: 集合名稱
//capped:是否啟用集合限制永罚,如果開啟需要制定一個限制條件啤呼,默認為不啟用,這個參數(shù)沒有實際意義
//max:集合中最大條數(shù)限制呢袱,默認為沒有限制
//size:限制集合使用空間的大小官扣,默認為沒有限制,size的優(yōu)先級比max要高
//autoIndexId:是否使用_id作為索引羞福,默認為使用(true或false)
4.集合操作
db.collection.drop() //刪除集合
5.文檔操作
- 新增
db.collection.insert({"name": "a","age":"b"}) //向集合插入一條數(shù)據(jù)(文檔)
- 修改
db.collection.update( //修改文檔
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
//query : update的查詢條件惕蹄,類似sql update查詢內(nèi)where后面的。
//update : update的對象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內(nèi)set后面的
//upsert : 可選卖陵,這個參數(shù)的意思是恋昼,如果不存在update的記錄,是否插入objNew,true為插入赶促,默認是false液肌,不插入。
//multi : 可選鸥滨,mongodb 默認是false,只更新找到的第一條記錄嗦哆,如果這個參數(shù)為true,就把按條件查出來多條記錄全部更新。
//writeConcern :可選婿滓,拋出異常的級別老速。
db.collection.save( //替換文檔
<document>,
{
writeConcern: <document>
}
)
//document : 文檔數(shù)據(jù)。
//writeConcern :可選凸主,拋出異常的級別橘券。
- 刪除
db.collection.remove( //刪除文檔
<query>,
{
justOne: <boolean>,
writeConcern: <document>
}
//query :(可選)刪除的文檔的條件。
//justOne : (可選)如果設為 true 或 1卿吐,則只刪除一個文檔旁舰。
//writeConcern :(可選)拋出異常的級別。
db.col.remove({}) //清空集合 "col" 的數(shù)據(jù)
- 查詢
db.collection.find(query, projection).pretty() 查詢文檔
db.collection.findOne() 查詢一個文檔
query :可選嗡官,使用查詢操作符指定查詢條件
projection :可選箭窜,使用投影操作符指定返回的鍵。查詢時返回文檔中所有鍵值衍腥, 只需省略該參數(shù)即可(默認省略)
pretty() 方法以格式化的方式來顯示所有文檔磺樱。
補充一下 projection 參數(shù)的使用方法
db.collection.find(query, projection)
若不指定 projection,則默認返回所有鍵婆咸,指定 projection 格式如下竹捉,有兩種模式
db.collection.find(query, {title: 1, by: 1}) // inclusion模式 指定返回的鍵,不返回其他鍵
db.collection.find(query, {title: 0, by: 0}) // exclusion模式 指定不返回的鍵,返回其他鍵
_id 鍵默認返回尚骄,需要主動指定 _id:0 才會隱藏
兩種模式不可混用(因為這樣的話無法推斷其他鍵是否應返回) 錯誤
db.collection.find(query, {title: 1, by: 0})
只能全1或全0块差,除了在inclusion模式時可以指定_id為0 正確
db.collection.find(query, {_id:0, title: 1, by: 1})
- MongoDB 的條件語句查詢:
- AND 和 OR 聯(lián)合使用(and用","表示乖仇,OR用"$or"表示)
操作 | 格式 | 范例 | RDBMS中的類似語句 |
---|---|---|---|
等于 | {<key>:<value>} | db.col.find({"by":"菜鳥教程"}).pretty() | where by = '菜鳥教程' |
小于 | {<key>:{$lt:<value>}} | db.col.find({"likes":{$lt:50}}).pretty() | where likes < 50 |
小于或等于 | {<key>:{$lte:<value>}} | db.col.find({"likes":{$lte:50}}).pretty() | where likes <= 50 |
大于 | {<key>:{$gt:<value>}} | db.col.find({"likes":{$gt:50}}).pretty() | where likes > 50 |
大于或等于 | {<key>:{$gte:<value>}} | db.col.find({"likes":{$gte:50}}).pretty() | where likes >= 50 |
不等于 | {<key>:{$ne:<value>}} | db.col.find({"likes":{$ne:50}}).pretty() | where likes != 50 |
類似常規(guī) SQL 語句為: 'where likes>50 AND (by = '菜鳥教程' OR title = 'MongoDB 教程')'
db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鳥教程"},{"title": "MongoDB 教程"}]}).pretty()
{
"_id" : ObjectId("56063f17ade2f21f36b03133"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一個 Nosql 數(shù)據(jù)庫",
"by" : "菜鳥教程",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
- $type 操作符
類型 | 數(shù)字 | 備注 |
---|---|---|
Double | 1 | |
String | 2 | |
Object | 3 | |
Array | 4 | |
Binary data | 5 | |
Undefined | 6 | 已廢棄憾儒。 |
Object id | 7 | |
Boolean | 8 | |
Date | 9 | |
Null | 10 | |
Regular Expression | 11 | |
JavaScript | 13 | |
Symbol | 14 | |
JavaScript | (with scope) 15 | |
32-bit integer | 16 | |
Timestamp | 17 | |
64-bit integer | 18 | |
Min key | 255 | Query with -1. |
Max key | 127 |
使用方式
db.col.find({"title" : {$type : 2}}) //選擇類型對應的數(shù)字進行查詢
- Link與Skip方法
db.collection_name.find().limit(number)
limit()方法接受一個數(shù)字參數(shù),該參數(shù)指定從MongoDB中讀取的記錄條數(shù)乃沙。
db.collection_name.find().limit(number).skip(number)
skip()方法來跳過指定數(shù)量的數(shù)據(jù)起趾,skip方法同樣接受一個數(shù)字參數(shù)作為跳過的記錄條數(shù)。
兩者結(jié)合可以實現(xiàn)簡單分頁功能
這里我們假設查詢第100001條數(shù)據(jù)警儒,這條數(shù)據(jù)的Amount值是:2399927训裆,我們來寫兩條語句分別如下:
b.test.sort({"amount":1}).skip(100000).limit(10) //183ms
db.test.find({amount:{$gt:2399927}}).sort({"amount":1}).limit(10) //53ms
以上是不同寫法之間的性能對比
- 排序-sort方法
db.collection_name.find().sort({key:1})
key文件的鍵名稱
sort()方法可以通過參數(shù)指定排序的字段眶根,并使用 1 和 -1 來指定排序的方式,其中 1 為升序排列边琉,而-1是用于降序排列属百。