插入文档(插入数据库) 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 }

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

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

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
