insert、remove、drop、update、find、show dbs、show tables先用一段简单的实际操作阐述下使用方法,再较详细的分析。 Linux下执行mongodb自带的mongo命令就可以进入类似mysql一样的控制界面,mongodb的数据库、集合、文档类似mysql中的数据库、表、记录的
insert、remove、drop、update、find、show dbs、show tables先用一段简单的实际操作阐述下使用方法,再较详细的分析。
Linux下执行mongodb自带的mongo命令就可以进入类似mysql一样的控制界面,mongodb的数据库、集合、文档类似mysql中的数据库、表、记录的概念,下面上干货。
#查看数据库 > show dbs; admin (empty) local 0.078GB myinfo 0.078GB #切换数据库,如果数据库不存在,将会在增加第一条记录时自动创建该数据库 > use myinfo switched to db myinfo #查看集合,在向一个不存在的集合添加文档的时自动创建该集合 > show tables; system.indexes #定义一个文档 > doc01={'id':'10', 'name':'job', 'doc':'hello world!'} { "id" : "10", "name" : "job", "doc" : "hello world!" } #查看文档 > doc01 { "id" : "10", "name" : "job", "doc" : "hello world!" } #将文档插入testtable集合 > db.testtable.insert(doc01) WriteResult({ "nInserted" : 1 }) #也可以向集合直接插入文档 > db.testtable.insert({'id':'11', 'name':'jim', 'doc':'hi world!'}) WriteResult({ "nInserted" : 1 }) #再次查看集合时,会发现新创建的集合testtable > show tables; system.indexes testtable #查找集合testtable中的所有文档 > db.testtable.find() { "_id" : ObjectId("5476cd8e0074a24d1b6eaea7"), "id" : "10", "name" : "job", "doc" : "hello world!" } { "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" } #查找id为11的文档 >db.testtable.find({'id':'11'}) { "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" } #更新id为10的文档,将name改为kiki > db.testtable.update({'id':'10'},{'id':'10', 'name':'kiki', 'doc':'hello workd!'}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.testtable.find() { "_id" : ObjectId("5476cd8e0074a24d1b6eaea7"), "id" : "10", "name" : "kiki", "doc" : "hello workd!" } { "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" } #将id等于10的文档删除 > db.testtable.remove({'id':'10'}) WriteResult({ "nRemoved" : 1 }) > db.testtable.find() { "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" } >
insert( )函数
使用比较简单,直接插入或间接插入已定义的文档即可。
update( )函数
db.collection.update( criteria, objNew, upsert, multi ) update()函数接受以下四个参数: criteria : update的查询条件,类似sql update查询内where后面的。 objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的 upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。 multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
remove( )函数、dorp( )函数
使用 remove() 函数移除数据 如果你想移除"userdetails"集合中"user_id" 为 "testuser"的数据你可以执行以下命令: >db.userdetails.remove( { "user_id" : "testuser" } ) 删除所有数据 如果你想删除"userdetails"集合中的所有数据,可以执行以下命令: >db.userdetails.remove({}) 使用drop()删除集合 如果你想删除整个"userdetails"集合,包含所有文档数据,可以执行以下数据: >db.userdetails.drop() 使用dropDatabase()函数删除数据库 如果你想删除整个数据库的数据,你可以执行以下命令: >db.dropDatabase()
find( )函数
从集合中获取数据 如果你想在集合中读取所有的的数据,可以执行以下命令 >db.userdetails.find(); 类似于如下SQL查询语句: Select * from userdetails; 通过指定条件读取数据 如果我们想在集合"userdetails"中读取"education"为"M.C.A." 的数据,我们可以执行以下命令: >db.userdetails.find({"education":"M.C.A."}) 类似如下SQL查询语句: Select * from userdetails where education="M.C.A."; 通过条件操作符读取数据 MongoDB中条件操作符有: (>) 大于 - $gt (=) 大于等于 - $gte () 大于操作符 - $gt 如果你想获取"testtable"集合中"age" 大于22的数据,你可以使用以下命令: >db.testtable.find({age : {$gt : 22}}) 类似于SQL语句: Select * from testtable where age >22; MongoDB 使用 () 查询operator - $lt 和 $gt 如果你想获取"testtable"集合中"age" 大于17以及小于24的数据,你可以执行以下命令: >db.testtable.find({age : {$lt :24, $gt : 17}}) 更多的查询技巧可以查看http://www.w3cschool.cc/mongodb/mongodb-query.html
文章出处:http://www.xiaomastack.com/2014/11/29/mongodb/

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)長度,始終使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

mysqlStringTypesIncludeVarChar,文本,char,Enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptoPuptOuptoPepePecifiedLimit.2)textisidealforlargetStortStorStoverStoverStorageWithoutAutAdefinedLength.3)charlisfixed-lenftenge,for forConsistentDatalikeCodes.4)

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)創建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。 2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。 3)GrantWriteAccessto'

MySQL中的字符串數據類型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序規則(Collations)決定了字符串的比較和排序方式。 1.CHAR適合固定長度字符串,VARCHAR適合可變長度字符串。 2.BINARY和VARBINARY用於二進制數據,BLOB和TEXT用於大對像數據。 3.排序規則如utf8mb4_unicode_ci忽略大小寫,適合用戶名;utf8mb4_bin區分大小寫,適合需要精確比較的字段。

最佳的MySQLVARCHAR列長度選擇應基於數據分析、考慮未來增長、評估性能影響及字符集需求。 1)分析數據以確定典型長度;2)預留未來擴展空間;3)注意大長度對性能的影響;4)考慮字符集對存儲的影響。通過這些步驟,可以優化數據庫的效率和擴展性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

禪工作室 13.0.1
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。