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/

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
