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

首頁 > 學院 > 開發(fā)設計 > 正文

MongoDB 文檔字段增刪改

2019-11-08 20:54:55
字體:
供稿:網(wǎng)友

MongoDB 基于CRUD(create,read,update,delete)方式實現(xiàn)了對集合上的文檔進行增刪改查。對于集合上字段的增刪改,可以使用set或者unset修改器來實現(xiàn)。也可以使用文檔替換的方式來實現(xiàn)。本文主要描述集合上字段的增刪改,以及基于選項upsert的更新。

關(guān)于MongoDB文檔更新可以參考:MongoDB 文檔更新

一、語法描述

db.collection.update( <query>, //查詢或過濾條件 <update>, //修改器(被修改鍵及內(nèi)容) { upsert: <boolean>, //為true或者false,如果為true,未找到匹配文檔則創(chuàng)建新文檔 multi: <boolean>, //用于確定是單行還是更新所有行(true為所有行) writeConcern: <document> //設定寫關(guān)注,用于確保強一致性還是弱一致性 } //后面的3.2之后的語法參數(shù)基本相同 ) 其他的如updateOne,updateMany等用法請參考:MongoDB 文檔更新 MongoDB集合上所有的寫操作特性 原子性操作(單個文檔級別原子性操作) _id 字段無法修改,即無法使用一個新的_id值來代替 由于更新導致文檔尺寸超出預期分配的情形,會自動調(diào)整填充因子,重新分配空間 保留文檔字段的順序,但是更新或重命名可能導致字段順序重新排序(_id總是文檔第一個字段)

二、update的幾個常用修改器

1、文檔更新($set修改器常規(guī)更新)

//$set修改器最常用,等同于RDBMS update的set子句 //演示重用的的示例集合數(shù)據(jù)請參考:mongoDB 比較運算符

> db.persons.find().limit(1).QQ.com", "score" : { "c" : 89, "m" : 96, "e" : 87 }, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "blog" : "http://blog.csdn.net/leshami"}> > //使用$set修改器修改age字段> db.persons.update({name:"robinson.cheng"},{$set:{age:24}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> //使用$set修改器修改嵌套文檔,使用成員.方式來實現(xiàn)> db.persons.update({name:"robinson.cheng"},{$set:{"score.c":92}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> //查看修改后的結(jié)果> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,age:1,score:1}).pretty(){ "name" : "robinson.cheng", "age" : 24, "score" : { "c" : 92, "m" : 96, "e" : 87 }}

2、將文檔普通字段轉(zhuǎn)換為數(shù)組($set)

//如下,將country設定為數(shù)組> db.persons.update({name:"robinson.cheng"},{$set:{country:["USA","CN","HK"]}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,country:1}){ "name" : "robinson.cheng", "country" : [ "USA", "CN", "HK" ] }

3、文檔新增字段($set實現(xiàn))

//下面使用$set文檔新增一個add字段,也可以使用$inc實現(xiàn)新增字段(見后面的描述)> db.persons.update({name:"robinson.cheng"},{$set:{add:"ShenZhen"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> //查看新增字段add> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,age:1,add:1}){ "name" : "robinson.cheng", "age" : 24, "add" : "ShenZhen" }

4、文檔刪除字段

//注,字段的刪除方法為{"$unset":{field_name:1}}> db.persons.update({name:"robinson.cheng"},{"$unset":{add:1}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })//驗證刪除后的結(jié)果add未顯示> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,age:1,add:1}){ "name" : "robinson.cheng", "age" : 24 }

5、字段值的增加或減少

//當使用$inc修改器時,當字段不存在時,會自動創(chuàng)建該字段,如果存在,則在原有值的基礎(chǔ)上進行增加或者減少//$inc主要是用于專門進行數(shù)字的增加或減少,因此$inc只能用于整型,長整形,或者雙精度浮點型的值//$inc不支持字符串,數(shù)組以及其他非數(shù)字的值//注,對于$inc的操作,$set也可以完成。$inc存在的理由是$inc更高效//下面通過$inc新增salary字段> db.persons.update({name:"robinson.cheng"},{$inc:{salary:1000}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,salary:1}).pretty(){ "name" : "robinson.cheng", "salary" : 1000 }//再次執(zhí)行$inc> db.persons.update({name:"robinson.cheng"},{$inc:{salary:2000}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })//查詢結(jié)果為在原有1000的基礎(chǔ)上增加2000,即為3000> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,salary:1}).pretty(){ "name" : "robinson.cheng", "salary" : 3000 }//基于$inc的負值> db.persons.update({name:"robinson.cheng"},{$inc:{salary:-1500}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })//負值后的結(jié)果> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,salary:1}).pretty(){ "name" : "robinson.cheng", "salary" : 1500 }//下面使用非數(shù)字來實現(xiàn)$inc,報錯如下> db.persons.update({name:"robinson.cheng"},{$inc:{salary:"1.5k"}})WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 14, "errmsg" : "Cannot increment with non-numeric argument: {salary: /"1.5k/"}" }})

6、時間戳字段的增加及自動更新($currentDate)

//有時候需要為文檔增加最后的更新時間自動,可以使用$currentDate方式來實現(xiàn)//下面為文檔增加lastModified時間戳字段> db.persons.update({name:"robinson.cheng"},{$inc:{salary:1000},$currentDate: {lastModified:true}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })//查看結(jié)果> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,salary:1,lastModified:1}){ "name" : "robinson.cheng", "salary" : 2500, "lastModified" : ISODate("2017-02-08T08:23:38.361Z") }//再次更新> db.persons.update({name:"robinson.cheng"},{$inc:{salary:500},$currentDate: {lastModified:true}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })//再次查看結(jié)果,時間戳自動被自動更新為最新的時間> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,salary:1,lastModified:1}){ "name" : "robinson.cheng", "salary" : 3000, "lastModified" : ISODate("2017-02-08T08:25:26.865Z") }

7、文檔字段重命名($rename)

//下面使用$rename對文檔字段重命名> db.persons.update({name:"robinson.cheng"},{$rename:{"name":"ename"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.persons.find({ename:"robinson.cheng"},{"_id":0,ename:1}){ "ename" : "robinson.cheng" }//對子文檔字段進行重命名> db.persons.update({ename:"robinson.cheng"},{$rename:{"score.c":"score.chinese"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.persons.find({ename:"robinson.cheng"},{"_id":0,ename:1,score:1}){ "score" : { "m" : 96, "e" : 87, "chinese" : 92 }, "ename" : "robinson.cheng" }//對整個集合上所有文檔字段進行重命名> db.persons.count()12> db.persons.update({},{$rename:{"name":"ename"}},{multi:true})WriteResult({ "nMatched" : 12, "nUpserted" : 0, "nModified" : 11 }) //此次修改為11條,因為前面以及修改過1條

三、upsert選項用法

// upsert相當于Oracle的merge into或者MySQL中的replace into// upsert即是當集合中匹配到滿足條件的文檔時,則更新文檔,否則則是新增文檔。前提是該選項的值為true,缺省為flase。> //下面的演示的是匹配到文檔時的例子> db.persons.update({name:"robinson.cheng"},{$set:{salary:4000}},{upsert:true})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) //此時提示有一個匹配,有一個被更新> db.persons.find({name:"robinson.cheng"},{"_id":0,name:1,salary:1}){ "name" : "robinson.cheng", "salary" : 4000 }> //下面通過upsert方式來新增文檔> db.persons.find({name:"thomas"}) //查找thomas> db.persons.update({name:"thomas"},{$set:{salary:4000,country:"USA",age:25}},{upsert:true})WriteResult({ "nMatched" : 0, "nUpserted" : 1, //此處結(jié)果表面有一個upserted,即沒有對應得文檔,更新的內(nèi)容作為一個新文檔插入到集合 "nModified" : 0, "_id" : ObjectId("589ae6f4e3a46ff8c567f1bf")})> db.persons.find({name:"thomas"}){ "_id" : ObjectId("589ae6f4e3a46ff8c567f1bf"), "name" : "thomas", "salary" : 4000, "country" : "USA", "age" : 25 }

四、小結(jié)

a、對于文檔上數(shù)據(jù)的修改有多種方式(修改器),常用的為$set修改器以及$inc b、$inc是一種高效的數(shù)據(jù)修改器,通常用于實現(xiàn)數(shù)值的增加或減少,僅支持數(shù)據(jù)類型。 c、對于文檔字段的增加,可以使用$set,$unset,$inc,$currentDate等方式 d、對于文檔字段的刪除,使用$unset方式來實現(xiàn) e、upsert選項可以實現(xiàn)匹配的文檔則更新,不匹配時則插入

DBA牛鵬社(SQL/NOSQL/LINUX)


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 太白县| 横山县| 雷州市| 濉溪县| 新化县| 西峡县| 香格里拉县| 监利县| 奈曼旗| 蓬安县| 昌都县| 乌拉特前旗| 金溪县| 南投县| 贵阳市| 广宗县| 历史| 保康县| 雷山县| 施甸县| 叙永县| 绥江县| 卢氏县| 淮安市| 额敏县| 九龙县| 吴川市| 呼和浩特市| 金湖县| 胶南市| 台南市| 繁昌县| 德保县| 绿春县| 璧山县| 绿春县| 九台市| 汉沽区| 丰都县| 伊宁县| 二连浩特市|