国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

mongodb

2019-11-08 20:47:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

通過(guò)上面的客戶(hù)端連接結(jié)果可以看到,默認(rèn)情況下會(huì)連接到 test這個(gè)數(shù)據(jù)庫(kù) 。 如果我們想要知道m(xù)ongodb現(xiàn)在有多少數(shù)據(jù)庫(kù),可以通過(guò)命令

> show dbsadmin   (empty)foobar  0.203GBlocal   0.078GBpiedra  0.078GB>

如果想要知道當(dāng)前是哪個(gè)數(shù)據(jù)庫(kù),使用 db 命令

> dbtest

如果想要切換到其他的數(shù)據(jù)庫(kù), 使用命令 user <your-dbname>

> use piedraswitched to db piedra

顯示當(dāng)前數(shù)據(jù)庫(kù)有哪一些集合show collections

> show collectionssystem.indexesusers

當(dāng)然,如果你第一次使用運(yùn)行 show collection ,結(jié)果是空的,但是當(dāng)你往集合里面插入數(shù)據(jù)后,就可以看到集合以及對(duì)應(yīng)的數(shù)據(jù)庫(kù)都會(huì)被創(chuàng)建。

> show dbsadmin   (empty)foobar  0.203GBlocal   0.078GBpiedra  0.078GB> use demoswitched to db demo> show collections> db.users.insert({username:"linwenbin",pwd:"1234"})WriteResult({ "nInserted" : 1 })> db.users.find(){ "_id" : ObjectId("55717e7ae25992bae59cca65"), "username" : "linwenbin", "pwd" : "1234" }>

在上面的操作中,我們切換到demo數(shù)據(jù)庫(kù),并且創(chuàng)建了users這個(gè)集合,還往users集合插入一條數(shù)據(jù)。

mongodb中的集合和MySQLOracle中的table是一樣的概念。

插入數(shù)據(jù)


在mongodb中,插入數(shù)據(jù)使用命令 db.collectionName.insert({data}) 或者 db.collectionName.save({data})這兩個(gè)方法都可以正常工作。 對(duì)于 insert方法在上文中已經(jīng)提到,這里演示save方法的使用。

> dbdemo> show collectionssystem.indexesusers>> db.users.save({username:"saveMethod",pwd:"123"}) WriteResult({ "nInserted" : 1 })

需要注意的是 save 方法在沒(méi)有指定 _id的時(shí)候,工作方式和insert是一樣的,但是如果指定了 _id 那么如果集合中已經(jīng)有對(duì)應(yīng)的_id了,就會(huì)用新的文檔覆蓋掉舊的文檔。修改數(shù)據(jù)


要修改數(shù)據(jù),也有兩種方式,一個(gè)是修改整個(gè)文檔,一個(gè)是修改部分。首先先介紹修改整個(gè)文檔的做法, 我們先把usres這個(gè)集合的數(shù)據(jù)都查詢(xún)出來(lái),方便和修改操作后的對(duì)比

> db.users.find(){ "_id" : ObjectId("55717e7ae25992bae59cca65"), "username" : "linwenbin", "pwd" : "1234" }{ "_id" : ObjectId("55717fd5e25992bae59cca66"), "username" : "saveMethod", "pwd" : "123" }>

第一種方式: db.collectionName.update({criteria},{data}); 這個(gè)方法會(huì)替換整個(gè)文檔。

> db.users.update({username:"linwenbin"},{age:22})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.find({username:'linwenbin'})>

可以發(fā)現(xiàn),當(dāng)我們修改完之后,再去查找username為’linwenbin’的數(shù)據(jù)就不存在了。

第二種方式: db.collectionName.update({criteria},{set:{newData}}); 現(xiàn)在我們對(duì)另一條數(shù)據(jù)進(jìn)行set的修改操作

> db.users.update({username:"saveMethod"},{$set:{pwd:"9999"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.find(){ "_id" : ObjectId("55717e7ae25992bae59cca65"), "age" : 22 }{ "_id" : ObjectId("55717fd5e25992bae59cca66"), "username" : "saveMethod", "pwd" : "9999" }>

可以觀(guān)察到,username為 saveMethod的那條數(shù)據(jù)的pwd已經(jīng)被成功修改為 ‘9999’了,而且其他屬性并沒(méi)有被新數(shù)據(jù)覆蓋掉。

查詢(xún)數(shù)據(jù)


mongodb的查詢(xún)方法就是find方法了,上文中已經(jīng)提到多次, db.collectionName.find({criteria})

> db.users.find({age:22}){ "_id" : ObjectId("55717e7ae25992bae59cca65"), "age" : 22 }>

如果都不指定find方法的條件,那么就是查詢(xún)所有的數(shù)據(jù)了。我們還可以通過(guò)指定查詢(xún)結(jié)果顯示哪一些字段,這個(gè)請(qǐng)查閱mongodb的manual手冊(cè)。刪除數(shù)據(jù)


如果要?jiǎng)h除某一個(gè)集合,可以通過(guò) db.collectionName.drop(); 來(lái)刪除。如果刪除某一個(gè)集合內(nèi)的數(shù)據(jù),則通過(guò) db.collectionName.remove({criteria});我們?cè)谏厦娌僮鞯幕A(chǔ)上,將 {age:22} 這條數(shù)據(jù)刪除

> db.users.find(){ "_id" : ObjectId("55717e7ae25992bae59cca65"), "age" : 22 }{ "_id" : ObjectId("55717fd5e25992bae59cca66"), "username" : "saveMethod", "pwd" : "9999" }> db.users.remove({age:22})WriteResult({ "nRemoved" : 1 })> db.users.find(){ "_id" : ObjectId("55717fd5e25992bae59cca66"), "username" : "saveMethod", "pwd" : "9999" }>

MongoDB索引


什么是索引?


索引是創(chuàng)建在表格之上的,對(duì)用戶(hù)來(lái)說(shuō)不可見(jiàn)。索引加快了查找的速度,但是會(huì)增加額外的空間,所以創(chuàng)建所以要謹(jǐn)慎,mongodb也對(duì)每一個(gè)Collection的索引個(gè)數(shù)有限制。

簡(jiǎn)單的說(shuō): 索引就是提供了一個(gè)能夠更快的定位到數(shù)據(jù)的方法

mongodb怎么查詢(xún)索引


要查看當(dāng)前集合有哪些索引,可以通過(guò)命令 db.collectionName.getIndexes();

> db.users.getIndexes()[        {                "v" : 1,                "key" : {                        "_id" : 1                },                "name" : "_id_",                "ns" : "demo.users"        }]>

key為 _id 為默認(rèn)的主鍵索引,創(chuàng)建集合的時(shí)候mongodb自動(dòng)為我們創(chuàng)建。

mongodb怎么創(chuàng)建索引


那如果此時(shí)我們想要對(duì)users這個(gè)集合的 username創(chuàng)建索引,應(yīng)該怎么操作?

> db.users.ensureIndex({username:1}){        "createdCollectionAutomatically" : false,        "numIndexesBefore" : 1,        "numIndexesAfter" : 2,        "ok" : 1}

這樣就表示我們創(chuàng)建索引成功了,在來(lái)看看usres集合的索引。

> db.users.getIndexes()[        {                "v" : 1,                "key" : {                        "_id" : 1                },                "name" : "_id_",                "ns" : "demo.users"        },        {                "v" : 1,                "key" : {                        "username" : 1                },                "name" : "username_1",                "ns" : "demo.users"        }]>

我們發(fā)現(xiàn),多了一個(gè)key為 username的索引。 其中創(chuàng)建索引 db.users.ensuerIndex({username:1}) 中的1 表示正序索引, -1表示逆序索引

mongodb怎么刪除索引


要?jiǎng)h除索引通過(guò) db.collectionName.dropIndex(column) 來(lái)刪除

> db.users.dropIndex({username:1}){ "nIndexesWas" : 2, "ok" : 1 }> db.users.getIndexes()[        {                "v" : 1,                "key" : {                        "_id" : 1                },                "name" : "_id_",                "ns" : "demo.users"        }]>

至此,簡(jiǎn)單的索引操作也記錄完成了。


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 祁门县| 沂水县| 治县。| 甘谷县| 台中县| 梅州市| 揭东县| 奈曼旗| 犍为县| 太原市| 尖扎县| 鄯善县| 四子王旗| 涡阳县| 随州市| 新野县| 温州市| 武川县| 丹巴县| 安乡县| 扎鲁特旗| 台北县| 长岛县| 房山区| 饶阳县| 闸北区| 琼海市| 汾西县| 曲阳县| 沁阳市| 安吉县| 伊吾县| 屏南县| 漠河县| 深水埗区| 临潭县| 易门县| 长泰县| 东丽区| 深州市| 沧州市|