前面介紹瞭如何啓動MongoDB以及用shell連接, 下面介紹一些我們在mysql中所熟悉的一些操作
1. 創建數據庫
MongoDB 創建數據庫的語法格式如下:
```
use DATABASE_NAME
```
如果數據庫不存在,則創建數據庫,否則切換到指定數據庫。
演示
```shell
> use zxmantou
switched to db zxmantou
> db
zxmantou
>
```
可見我們當前的db已經是zxmantou了, 使用**show dbs**命令查看數據庫列表
```shell
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
```
發現新增的數據庫並不在其中. 要顯示它,我們需要向 zxmantou 數據庫插入一些數據。
```shell
> db.user.insert({name:"zxmantou"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
zxmantou 0.000GB
>
```
刪除數據庫
刪除當前數據庫,默認爲 test, 下面實例中將刪除我們新建的zxmantou庫:
> show dbs admin 0.000GB local 0.000GB test 0.000GB zxmantou 0.000GB > db zxmantou > db.dropDatabase() { "dropped" : "zxmantou", "ok" : 1 } > show dbs admin 0.000GB local 0.000GB test 0.000GB
刪除集合(刪除數據表)
集合刪除語法格式如下:db.COLLECTION_NAME.drop()
實例:
> use zxmantou switched to db zxmantou > show tables user > db.user.drop() true > show tables >
插入文檔(新增記錄)
MongoDB 使用 insert() 或 save() 方法向集合中插入文檔,語法如下:db.COLLECTION_NAME.insert(document)
實例:
> use zxmantou switched to db zxmantou > db.user.insert({ ... name:"zxmantou", ... age:"27", ... gender: "male", ... site: "www.zhangyuxiaojiao.com" ... }) WriteResult({ "nInserted" : 1 }) > db.user.find() { "_id" : ObjectId("58e4d75bbe3140034aeb2ba1"), "name" : "zxmantou", "age" : "27", "gender" : "male", "site" : "www.zhan gyuxiaojiao.com" } >
以上實例中 user 是我們的集合名(數據表名),如果該集合不在該數據庫中, MongoDB 會自動創建該集合並插入文檔(一行記錄)。
更新文檔(更新記錄)
MongoDB 使用 update() 和 save() 方法來更新集合中的文檔。接下來讓我們詳細來看下兩個函數的應用及其區別。5.1 Update()方法:
update() 方法用於更新已存在的文檔。語法格式如下:``` db.COLLECTION_NAME.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } ) ```
參數說明: query : update的查詢條件,類似sql update查詢內where後面的。 update : update的對象和一些更新的操作符(如$,$inc...)等,也可以理解爲sql update查詢內set後面的 upsert : 可選,這個參數的意思是,如果不存在update的記錄,是否插入objNew,true爲插入,默認是false,不插入。 multi : 可選,mongodb 默認是false,只更新找到的第一條記錄,如果這個參數爲true,就把按條件查出來多條記錄全部更新。 writeConcern :可選,拋出異常的級別。
我們剛纔已經插入了一條記錄, 現在對其進行修改:
> db.user.find() { "_id" : ObjectId("58e4d75bbe3140034aeb2ba1"), "name" : "zxmantou", "age" : "27", "gender" : "male", "site" : "www.zhan gyuxiaojiao.com" } > db.user.update({'name':'zxmantou'},{$set:{'age':18}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.user.find().pretty() { "_id" : ObjectId("58e4d75bbe3140034aeb2ba1"), "name" : "zxmantou", "age" : 18, "gender" : "male", "site" : "www.zhangyuxiaojiao.com" } >
可見年齡已經年輕到18歲啦~
5.2 save() 方法
save() 方法通過傳入的文檔來替換已有文檔。語法格式如下:db.collection.save( <document>, { writeConcern: <document> } )
參數說明: document : 文檔數據。 writeConcern :可選,拋出異常的級別。
實例
以下實例中我們替換了 _id 爲 58e4d75bbe3140034aeb2ba1 的文檔數據:
> db.user.save({ ... "_id" : ObjectId("58e4d75bbe3140034aeb2ba1"), ... "name" : "zhangyuxiaojiao", ... "gender" : "male", ... "age" : "27", ... "site" : "zxmantou.github.io" ... }) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.user.find().pretty() { "_id" : ObjectId("58e4d75bbe3140034aeb2ba1"), "name" : "zhangyuxiaojiao", "gender" : "male", "age" : "27", "site" : "zxmantou.github.io" } >
更多實例
只更新第一條記錄:
db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );
全部更新:
db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
只添加第一條:
db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
全部添加加進去:
db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );
全部更新:
db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );
只更新第一條記錄:
db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );
刪除文檔(刪除記錄)
remove() 方法的基本語法格式如下所示:db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document> } )
參數說明: query :(可選)刪除的文檔的條件。 justOne : (可選)如果設爲 true 或 1,則只刪除一個文檔。 writeConcern :(可選)拋出異常的級別。
實例:
> db.user.find().pretty()
{
"_id" : ObjectId("58e4d75bbe3140034aeb2ba1"),
"name" : "zhangyuxiaojiao",
"gender" : "male",
"age" : "27",
"site" : "zxmantou.github.io"
}
> db.user.remove({'name':'zhangyuxiaojiao'})
WriteResult({ "nRemoved" : 1 })
> db.user.find().pretty()
>
如果你想刪除所有數據,可以使用以下方式(類似常規 SQL 的 truncate 命令):
>db.col.remove({})
- 查詢文檔(查詢記錄)
之前實例已經演示了通過find()方法來查詢所有記錄的方法, 下一篇內容中將詳細介紹複雜的查詢方法