mongodb實(shí)踐 2_第1頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余1頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、mongodb實(shí)踐 2db.things.remove(i:1); db.things.(i:1); db.things.find(i:2); "_" : objectid("4ee8510a0c16000000006ec7"), "i" : 2, "i2" : 4 "_id" : objectid("4ee8514d0c16000000006b"), "i" : 2, "i2" : 4, "i3" : 8 db.t

2、hings.remove()會(huì)刪除things聚攏中的全部數(shù)據(jù)。 甚至可以刪除聚攏本身 db.things.drop() 更新數(shù)據(jù) *mongodb官方建議,假如僅僅是更改某些域,那么用法$modifiers 越發(fā)合適 寫道 a modifier up has the advantages of avoiding the latey involved in querying and returning the object. the modifier update ao features operation atomicity and very little network data ansf

3、er. *modifiers有如下操作 寫道 $inc 自增/自減 $ 賦值 $ 刪除某個(gè)列 $push 追加到原有值后 $pushall 追加數(shù)組到原有值后 $atoset and $each 向數(shù)組添加一個(gè)元素/向數(shù)組添加多個(gè)元素 $pop 移除某個(gè)數(shù)組元素(-1表示第一個(gè)、1表示最后一個(gè)) $pull 移除指定的數(shù)組元素 $pullall 移除多個(gè)指定的數(shù)組元素 $rename 更新域名 $bit 按位操作,僅限整型 *將客戶joy的年齡+1 db.customers.find(name:"joy"); "_id" : objectid("

4、;4ee98be10b240000000027f3"), "name" : "joy", "age" : 22 db.customers.update(name:"joy",$inc:age:1); db.customers.find(name:"joy"); "_id" : objectid("4ee98be10b240000000027f3"), "name" : "joy", "age&qu

5、ot; : 23 再添加兩條數(shù)據(jù) db.customers.save(name:"joy",age:23);db.customers.save(name:"joy",age:23); 如何將全部joy的年齡都變?yōu)?4呢? 試一下前面的辦法 db.customers.update(name:"joy",$inc:age:1); db.customers.find(name:"joy"); "_id" : objectid("4ee98be10b240000000027f3"),

6、 "name" : "joy", "age" : 24 "_id" : objectid("4ee99ce18b12000000004f86"), "name" : "joy", "age" : 23 "_id" : objectid("4ee99d0f8b12000000004f87"), "name" : "joy", "age" :

7、 23 很圓滿只轉(zhuǎn)變了第一條數(shù)據(jù):( update的語(yǔ)法為:db.customers.update(query, object, upsert_bool, multi_bool) ,有兩個(gè)可選參數(shù),我們沒(méi)實(shí)用到,官網(wǎng)上對(duì)這幾個(gè)參數(shù)的說(shuō)明是 寫道 query 查詢哪些記錄需要被更新 object 需要更新的域 upsert 假如為true則有記錄時(shí)更新記錄,沒(méi)有記錄時(shí)添加一條新記錄(默認(rèn)值似乎是true) multi 打算到底是更新全部查詢到的記錄還是只更新一條(默認(rèn)值似乎是false) 再試一下 db.customers.update(name:"joy",$inc:age

8、:1,true,true); db.customers.find(name:"joy"); "_id" : objectid("4ee98be10b240000000027f3"), "name" : "joy", "age" : 25 "_id" : objectid("4ee99ce18b12000000004f86"), "name" : "joy", "age" : 2

9、4 "_id" : objectid("4ee99d0f8b12000000004f87"), "name" : "joy", "age" : 24 全部名字叫joy的客戶的年齡都增大了一歲。 現(xiàn)在把25歲的joy改成24歲,可以這么來(lái) db.customers.update(name:"joy",age:25,$set:age:24); db.customers.find(name:"joy"); "_id" : objectid(&

10、quot;4ee98be10b240000000027f3"), "name" : "joy", "age" : 24 "_id" : objectid("4ee99ce18b12000000004f86"), "name" : "joy", "age" : 24 "_id" : objectid("4ee99d0f8b12000000004f87"), "name"

11、 : "joy", "age" : 24 這樣在customers中就有三個(gè)名字和年齡都相同的客戶了:) 添加一共性別域 db.customers.update(name:"joy",age:24,$set:gender:"m",true,true); db.customers.find(name:"joy"); "_id" : objectid("4ee98be10b240000000027f3"), "age" : 24, &quo

12、t;gender" : "m", "name" : "joy" "_id" : objectid("4ee99ce18b12000000004f86"), "age" : 24, "gender" : "m", "name" : "joy" "_id" : objectid("4ee99d0f8b12000000004f87"), "ag

13、e" : 24, "gender" : "m", "name" : "joy" 刪除該域 db.customers.update(name:"joy",age:24,$unset:gender:"m",true,true); db.customers.find(name:"joy"); "_id" : objectid("4ee98be10b240000000027f3"), "age"

14、 : 24, "name" : "joy" "_id" : objectid("4ee99ce18b12000000004f86"), "age" : 24, "name" : "joy" "_id" : objectid("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy" 經(jīng)過(guò)一些步驟,現(xiàn)在數(shù)據(jù)庫(kù)

15、中3個(gè)joy的年齡不同了,接下來(lái)給他們加一個(gè)數(shù)組域,記錄他們的訂單記錄 db.customers.update(name:"joy",$push:orders:"1000",true,true); db.customers.find(name:"joy"); "_id" : objectid("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders&quo

16、t; : "1000" "_id" : objectid("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : "1000" "_id" : objectid("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy

17、", "orders" : "1000" 再試試$pushall db.customers.update(name:"joy",age:22,$pushall:orders:"1001","1002",true,true); db.customers.find(name:"joy"); "_id" : objectid("4ee98be10b240000000027f3"), "age" : 22, &qu

18、ot;name" : "joy", "orders" : "1000", "1001", "1002" "_id" : objectid("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : "1000" "_id" : objectid(&quo

19、t;4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : "1000" 試試$addtoset and $each db.customers.update(name:"joy",age:22,$addtoset:orders:$each:"1003","1004",true,true); db.customers.find(name:"j

20、oy"); "_id" : objectid("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : "1000" "_id" : objectid("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", &qu

21、ot;orders" : "1000" "_id" : objectid("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : "1000", "1001", "1002", "1003", "1004" $pop把數(shù)組元素彈出 db.customers.update

22、(name:"joy",age:22,$pop:orders:1,true,true); db.customers.find(name:"joy"); "_id" : objectid("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : "1000" "_id" : objectid("4ee99d0f8

23、b12000000004f87"), "age" : 24, "name" : "joy", "orders" : "1000" "_id" : objectid("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : "1000", "1001",

24、 "1002", "1003" db.customers.update(name:"joy",age:22,$pop:orders:-1,true,true); db.customers.find(name:"joy"); "_id" : objectid("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : &qu

25、ot;1000" "_id" : objectid("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : "1000" "_id" : objectid("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy",

26、"orders" : "1001", "1002", "1003" $push也可以把數(shù)組元素彈出 db.customers.update(name:"joy",age:22,$pull:orders:"1002",true,true); db.customers.find(name:"joy"); "_id" : objectid("4ee99ce18b12000000004f86"), "age&quo

27、t; : 23, "name" : "joy", "orders" : "1000" "_id" : objectid("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : "1000" "_id" : objectid("4ee98be10b240000000027

28、f3"), "age" : 22, "name" : "joy", "orders" : "1001", "1003" db.customers.update(name:"joy",age:22,$pullall:orders:"1001","1003",true,true); db.customers.find(name:"joy"); "_id" : object

29、id("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : "1000" "_id" : objectid("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : "1000"

30、"_id" : objectid("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : 把orders修改為customer_orders db.customers.update(name:"joy",$rename:orders:"customer_orders",true,true); invalid modifier specified $renam

31、e 很圓滿:(查了資料才發(fā)覺(jué)$rename這個(gè)在version 1.7.2+ 以后才有,而$bit這個(gè)指令version 1.7.5+之后才有。我的測(cè)試版本 db.version(); 1.6.5 *最后一點(diǎn),關(guān)于$的使用:$相當(dāng)實(shí)用,官網(wǎng)的說(shuō)明是 寫道 the $ operator (by itself) means "position of the matched array item in the query". use this to find an array member and then manipulate it. $指的就是在一次查詢中匹配的數(shù)組項(xiàng)的位置,通過(guò)用法$可以找到數(shù)組元素并操作它。(翻譯的不好,滿頭大汗:|) 定義一個(gè)文章+評(píng)論的數(shù)據(jù)模式 archive = title:"mongodb learning",author:"someone",comments:by:"joy",votes:1,by:"tim",votes:3 &qu

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論