搜索
首页数据库mysql教程mongoDB学习(二)之常用的修改操作

mongoDB学习(二)之常用的修改操作

Jun 07, 2016 pm 04:02 PM
mongodb修改学习插入操作数字文档实际的

插入文档(插入数据库) db.person.insert({_id:0001,nameyuexin}) 清除数据 db.person.drop() 批量插入文档 shell中不支持批量插入 完成批量插入使用for循环 for(var i=0;i10;i++){ .. db.persons.insert({_id:i,name:yuexin+i}) .. } save操作 save操作与in

插入文档(插入数据库)
db.person.insert({_id:"0001",name"yuexin"})
清除数据
db.person.drop()
批量插入文档
shell中不支持批量插入
完成批量插入使用for循环
for(var i=0;i .. db.persons.insert({_id:i,name:"yuexin"+i})
.. }

save操作
save操作与insert操作的区别是当id一样的时候save会变成更新操作而insert会报错


删除列表中所有数据
db.persons.remove()删除persons中的数据但是不删除索引(db.system.indexes.find()中有值)
对于
db.person.drop()会删除索引
删除带查询条件的
db.persons.remove({_id:"3"})

如果想清除一个数据量十分庞大的集合,直接删除该集合并且重新建立索引的办法要比直接用remove的效率高很多。

1.强硬的文档替换式更新操作
update更新
> db.persons.update({age:55},{name:"000"})
2.主键冲突时时候会报错并且停止更新操作
因为是强硬替换的文档和已有文档id冲突时时候会报错
3.insertOrUpdate操作
目的:查询器查询出来数据就执行更新操作,查不出来就替换操作
做法:> db.persons.update({name:"0004"},{name:"1114"},true)
4.批量更新
> db.persons.update({name:"yuexin"},{$set:{name:"text"}},false,true)
默认情况当查询器查出多条数据的时候默认就修改第一条数据。
db.[documentName].update({查询器},{修改器},false,true)

修改器:
$set 用来指定一个键值对,如果存在就进行修改,不存在就进行添加
$inc 只是使用于数字类型,他可以为指定的键对应的数字类型的数值做加减操作。
> db.persons.update({age:20},{$inc:{age:-2}})
$unset 删除指定的键
> db.persons.update({age:18},{$unset:{age:1}})
$push
1.如果指定的键是数组追加新的数值
> db.persons.insert({_id:0,name:0,book:[]})
> db.persons.find()
{ "_id" : 0, "name" : 0, "book" : [ ] }
> db.persons.update({name:0},{$push:{book:"abc"}})
> db.persons.find()
{ "_id" : 0, "book" : [ "abc" ], "name" : 0 }
> db.persons.update({name:0},{$push:{book:"abcd"}})
> db.persons.update({name:0},{$push:{book:"abcd"}})
> db.persons.find()
{ "_id" : 0, "book" : [ "abc", "abcd", "abcd" ], "name" : 0 }
2.如果指定的键不是数组,则中断当前操作
Cannot apply $push/$pushAll modifier to non-array
3.如果不存在指定的键则创建数组类型的键值对
> db.persons.update({name:0},{$push:{things:"abcd"}})
> db.persons.find()
{ "_id" : 0, "book" : [ "abc", "abcd", "abcd" ], "name" : 0, "things" : [ "abcd"
] }

$pushAll 与push相似,是批量加入数组数据
> db.persons.update({name:0},{$pushAll:{things:["abcd","01","02"]}})
> db.persons.find()
{ "_id" : 0, "book" : [ "abc", "abcd", "abcd" ], "name" : 0, "things" : [ "abcd"
, "abcd", "01", "02" ] }

$addToSet 目标数组存在此项则不操作,不存在此项则加进去
> db.persons.insert({_id:0,name:0,book:[]})
> db.persons.update({name:0},{$addToSet:{book:"abcd"}})
> db.persons.find()
{ "_id" : 0, "book" : [ "abcd" ], "name" : 0 }
> db.persons.update({name:0},{$addToSet:{book:"abcd"}})
> db.persons.find()
{ "_id" : 0, "book" : [ "abcd" ], "name" : 0 }

$pop 删除第一个或者最后一个(-1为第一个,1为最后一个)
> db.persons.update({name:0},{$pop:{book:-1}})

$pull 删除指定的一个
> db.persons.update({name:0},{$pull:{book:"01"}})

$pull 删除多个
> db.persons.update({name:0},{$pullAll:{book:["01","02"]}})

$ 数组定位器,如果数组有多个数值我们只想对其中一部分进行操作我们就要用到定位器($)(??)

修改器是放在最外面的,查询器是放在内层的

$addToSet与$each结合完成批量数组更新
有的话就不添加了
> db.persons.find()
{ "_id" : 0, "book" : [ "js" ], "name" : 0 }
> db.persons.update({_id:0},{$addToSet:{book:{$each:["js","db"]}}})
> db.persons.find()
{ "_id" : 0, "book" : [ "js", "db" ], "name" : 0 }

当document被创建的时候mongoDB为其分配内存和预留内存,当修改操作不超过预留内存的时候速度非常快,反而超过了就要分配新的内存则会消耗时间

runCommand可以执行mongoDB中的特殊函数
findAndModify就是特殊函数之一他的用于是返回update或remove后的文档
> db.persons.find()
{ "_id" : 0, "book" : [ "js", "db" ], "name" : 0 }
> ps = db.runCommand({
... "findAndModify":"persons",
... "query":{name:0},
... "update":{$set:{age:100}},
... new:true})
{
"lastErrorObject" : {
"updatedExisting" : true,
"n" : 1,
"connectionId" : 1,
"err" : null,
"ok" : 1
},
"value" : {
"_id" : 0,
"age" : 100,
"book" : [
"js",
"db"
],
"name" : 0
},
"ok" : 1
}
> ps.value
{ "_id" : 0, "age" : 100, "book" : [ "js", "db" ], "name" : 0 }
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。