一、前言
MongoDB 虽说是文档型数据库,但是在学习和使用其语法时发现又与 MySQL 有些相似之处,在此记录点滴日后复习。
二、概念区别
比较MySQLMongoDB
库databasedatabase
表tablecollection
行rowdocument
列columnfield
索引indexindex
表关联table joins$lookup
主键primary keyprimary key
聚合aggregationaggregation pipeline
三、命令区别
比较MySQLMongoDB
服务端mysqldmongod
客户端mysqlmongo
四、关键字和函数区别
MySQLMongoDB
where$match
group by$group
having$match
select$project
order by$sort
limit$limit
sum()$sum
count()$sum
join$lookup
五、语句区别
# 5.1 表结构
# 5.1.1 创建表/集合
db.people.insertOne( {
user_id:"abc123",
age:55,
status:"A"
} )
相当于
CREATETABLEpeople(
idMEDIUMINTNOTNULLAUTO_INCREMENT,
user_idVarchar(30),
ageNumber,
statuschar(1),
PRIMARYKEY(id)
)
# 5.1.2 新增字段
db.people.updateMany(
{ },
{ $set: { join_date:newDate() } }
)
相当于 ALTER TABLE people ADD join_date DATETIME
# 5.1.3 删除字段
db.people.updateMany(
{ },
{ $unset: {"join_date":""} }
)
相当于 ALTER TABLE people DROP COLUMN join_date
# 5.1.4 创建索引
db.people.createIndex( {user_id:1} )
相当于CREATEINDEXidx_user_id_ascONpeople(user_id)
# 5.1.5 删除表/集合
db.people.drop()
相当于DROPTABLEpeople
# 5.2 新增记录/文档
db.people.insertOne(
{user_id:"bcd001", age:45, status:"A"}
)
相当于INSERTINTOpeople(user_id,age,status)VALUES("bcd001",45,"A")
# 5.3 查询记录/文档
# 5.3.1 简单查询
db.people.find()
相当于SELECT*FROMpeople
db.people.find(
{ },
{user_id:1, status:1}
)
相当于SELECTid,user_id,statusFROMpeople
db.people.find(
{ },
{user_id:1, status:1, _id:0}
)
相当于SELECTuser_id,statusFROMpeople
# 5.3.2 条件查询
db.people.find(
{ status:"A"}
)
相当于 SELECT * FROM people WHERE status ="A"
db.people.find(
{ status:"A"},
{ user_id:1, status:1, _id:0}
)
相当于 SELECT user_id, status FROM people WHERE status ="A"
# 5.3.3 非查询
db.people.find(
{ status: { $ne:"A"} }
)
相当于 SELECT * FROM people WHERE status !="A"
# 5.3.4 且查询
db.people.find(
{ status:"A",
age:50}
)
相当于 SELECT * FROM people WHERE status ="A"ANDage =50
# 5.3.5 或查询
db.people.find(
{ $or: [ { status:"A"} ,
{ age:50} ] }
)
相当于 SELECT * FROM people WHERE status ="A"ORage =50
# 5.3.6 大于查询
db.people.find(
{age: { $gt:25} }
)
相当于SELECT*FROMpeopleWHEREage> 25
# 5.3.7 小于查询
db.people.find(
{age: { $lt:25} }
)
相当于SELECT*FROMpeopleWHEREage< 25
# 5.3.8 范围查询
db.people.find(
{ age: { $gt:25, $lte:50} }
)
相当于 SELECT * FROM people WHERE age >25ANDage <=50
# 5.3.9 模糊查询
db.people.find( {user_id: /bc/ } ) 或db.people.find( {user_id: { $regex: /bc/ } } )
相当于SELECT*FROMpeopleWHEREuser_idlike"%bc%"
db.people.find( {user_id: /^bc/ } ) 或db.people.find( {user_id: { $regex: /^bc/ } } )
相当于SELECT*FROMpeopleWHEREuser_idlike"bc%"
# 5.3.10 排序查询
db.people.find( { status:"A"} ).sort( { user_id:1} )
相当于 SELECT * FROM people WHERE status ="A"ORDER BY user_id ASC
db.people.find( { status:"A"} ).sort( { user_id:-1} )
相当于 SELECT * FROM people WHERE status ="A"ORDER BY user_id DESC
# 5.3.11 统计查询
db.people.count() 或db.people.find().count()
相当于SELECTCOUNT(*)FROMpeople
db.people.count( {user_id: { $exists: true } } ) 或db.people.find( {user_id: { $exists: true } } ).count()
相当于SELECTCOUNT(user_id)FROMpeople
db.people.count( {age: { $gt:30} } ) 或db.people.find( {age: { $gt:30} } ).count()
相当于SELECTCOUNT(*)FROMpeopleWHEREage> 30
# 5.3.12 去重查询
db.people.distinct( "status" )
相当于SELECTDISTINCT(status)FROMpeople
# 5.3.13 分页查询
db.people.findOne() 或db.people.find().limit(1)
相当于SELECT*FROMpeopleLIMIT1
db.people.find().limit(5).skip(10)
相当于SELECT*FROMpeopleLIMIT5SKIP10
# 5.3.14 查询计划
db.people.find( { status:"A"} ).explain()
相当于 EXPLAIN SELECT * FROM people WHERE status ="A"
# 5.4 修改记录/文档
db.people.updateMany(
{ age: { $gt:25} },
{ $set: { status:"C"} }
);
相当于 UPDATE people SET status ="C"WHERE age >25;
db.people.updateMany(
{ status:"A"} ,
{ $inc: { age:3} }
);
相当于 UPDATE people SET age = age +3WHERE status ="A";
# 5.5 删除记录/文档
db.people.deleteMany( { status:"D"} );
相当于 DELETE FROM people WHERE status ="D";
db.people.deleteMany({});
相当于 DELETE FROM people;
六、参考资料
https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/ 关键字和函数相关
https://docs.mongodb.com/manual/reference/sql-comparison/ 语句相关
源网络,版权归原创者所有。如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。
更多技术,欢迎关注下方公众号