search
HomeDatabaseMysql Tutorialmongodb基本增删改查

学习一个数据库,首先要会的是如何连接数据库,如何进行基本的增删改查,其它高级功能慢慢学习即可。 下面主要讲解mogodb基本的增删改查 1、创建数据库:use mkyongdb (有则跳转到这个数据库,没有的话创建并跳转) 显示所有数据库:show dbs 2、创建集合(

学习一个数据库,首先要会的是如何连接数据库,如何进行基本的增删改查,其它高级功能慢慢学习即可。

下面主要讲解mogodb基本的增删改查

1、创建数据库:use mkyongdb (有则跳转到这个数据库,没有的话创建并跳转)
显示所有数据库:show dbs
2、创建集合(相当于关系数据库的表):
集合操作:
(1)插入数据
db.users.insert({'name':'xumingxiang','sex':'man'}) 创建一个users的集合
显示当前数据库下的所有集合:show collections
显示users集合下的所有数据文档:db.users.find()
显示集合的记录数:db.users.count()
(2)更新数据
db.users.update({'name':'xiangshu'},{'$set':{'sex':'women'}},upsert=true,multi=false)
第一:查询的条件
第二:更新的字段
第三:如果不存在则插入
第四:是否允许修改多条记录
(3)删除数据
db. users.remove({'name':'xumingxiang'})
(4)删除所有记录
db.users.remove()
删除collection
db.users.drop() //如果删除成功会返回“true”,否则返回“false”
删除当前数据库
db.dropDatabase()


NOSQL和SQL数据库比较(PS:参照网上的)
MongoDB语法 MySql语法
db.test.find({'name':'foobar'}) select * from test where name='foobar'
db.test.find() select * from test
db.test.find({'ID':10}).count() select count(*) from test where ID=10
db.test.find().skip(10).limit(20) select * from test limit 10,20
db.test.find({'ID':{$in:[25,35,45]}}) select * from test where ID in (25,35,45)
db.test.find().sort({'ID':-1}) select * from test order by ID desc
db.test.distinct('name',{'ID':{$lt:20}}) select distinct(name) from test where ID db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) select name,sum(marks) from test group by name
db.test.find('this.ID select name from test where ID db.test.insert({'name':'foobar','age':25})insert into test ('name','age') values('foobar',25)
db.test.remove({}) delete * from test
db.test.remove({'age':20}) delete test where age=20
db.test.remove({'age':{$lt:20}}) elete test where age db.test.remove({'age':{$lte:20}}) delete test where age db.test.remove({'age':{$gt:20}}) delete test where age>20
db.test.remove({'age':{$gte:20}}) delete test where age>=20
db.test.remove({'age':{$ne:20}}) delete test where age!=20
db.test.update({'name':'foobar'},{$set:{'age':36}}) update test set age=36 where name='foobar'
db.test.update({'name':'foobar'},{$inc:{'age':3}}) update test set age=age+3 where name='foobar'


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

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

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

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

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

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

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

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

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

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: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

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

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

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

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

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.