MongoDB语法,含sql对比

一、可视化工具

我这里使用的是MongoDB Compass,官网自行下载安装即可,界面如下:

点击下面即可输入语句操作

二、数据库操作

首先说一下mongodb与mysql的区别:

MYSQLMongoDB
数据库类型关系型数据库, 存储的是结构化的表格数据非关系型数据库(nosql),文档型数据库,存储的是文档型数据(JSON格式),数据具备自述性,呈现分层的树状数据结构,数据结构由键值对组成
查询语言本文介绍本文介绍
数据模式需要预先定义字段动态模式,同一个集合里的文档不需要有相同的字段和结构
索引可以对表中的列建立索引可以对任何属性建立索引
延迟延迟相对较高写入操作有较低的延迟,非常适合实时应用
可靠性有完整的事务支持不支持事务操作
扩展性和性能使用垂直扩展的架构使用可水平扩展的架构
占用空间占用空间小占用空间大

 

数据库语句:

1.创建使用数据库:如果已有则切换到此,如果没有则自动创建
use demo
2.删除数据库
db.dropDatabase()
3.创建集合
db.createCollection('student')
4.删除集合
db.student.drop()

#sql:
drop table student;
5.插入
#插入一条数据
db.student.insertOne(
  { "name" : "John", "age": 30 }
)
​
#插入多条数据
db.student.insertMany(
[{ "name" : "Alice", "age": 25 }, { "name" : "Jack", "age": 17 }]
)
​
#插入一条或多条数据
db.student.insert(
[{ "name" : "Alice", "age": 25 }, { "name" : "Jack", "age": 17 }]
)


#sql
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);

 

6.查询

(1)查询所有数据

mongo: db.student.find();
sql: select * from student;

 (2)查询指定

db.student.find({"name": 'John'})  
select * from student where name='John'

(3)查询年龄大于23的记录

注:关系运算符  $gt:> 、 $lt:< 、 $gte:>= 、 $lte:<= 、  $ne: !=  

db.student.find({"age": {"$gt": 23}})
select * from student where age>23;

(4)and  or

# and   or
db.student.find({"name": "John", "age": 30})
select * from student where name=John and age=30;

db.student.find({"$or": [{"name": "John"}, {"age": 30}]})
select * from student where name=John or age=30;

(5)in   not in 

#in  
db.student.find({"age": {"$in": [30, 25]}})
select * from student where age in (25,30);

#not in
db.student.find({"age": {"$nin": [30, 25]}})
select * from student where age not in (30, 25);

(6)包含,模糊匹配 

#查询包含值的数据
db.student.find({"name": /值/})
select * from student where name like "%值%";

注:/就相当于%,使用模糊匹配时,值不能有双引号

#查询以某个值开头的数据
db.student.find({"name": /^值/})
select * from student where name like "值%";

#查询以某个值结尾的数据
db.student.find({"name": /值$/})
select * from student where name like "%值";

(7)升序 降序

db.student.find().sort({"age": 1})
select * from student order by age asc

db.student.find().sort({"age": -1})
select * from student order by age desc

(8)count

db.student.count();
select count(*) from student

(9)去重

去重
db.student.distinct('name');
select distinct name from student;

(10)limit

db.student.find().limit(10);
select * from student limit 10;

(11)查询 10 条以后的数据

db.student.find().skip(10)

查询6-10条数据: 执行顺序先执行skip(),然后执行 limit()。

db.student.find().limit(10).skip(5)
select * form student limit 5,5

(12)聚合求一列平均值

db.student.aggregate({$group:{_id:null,age:{$avg:"$age"}}})

7.更新修改
db.student.update({"键名":"值"},{"要修改的键":"要修改的值"})
注:只会修改第一条匹配条件的记录
update student set 字段名 = 值 where id=值

修改多条数据
db.student.update({"键名":"值"},{$set:{"要修改的键":"要修改的值"}},{multi:true})
update student set 字段名 = 值 where id=值
8.删除
删除所有数据
db.student.remove({});
delete from student;


根据条件删除
db.student.remove({"age":30});
delete from student where age=30;

  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoDB中,没有SQL语句,因为MongoDB是一个非关系型数据库,它使用一种称为MongoDB查询语言(MQL)的查询语言。MQL与SQL有所不同,但有一些相似之处。 要计算集合中文档的数量,你可以使用以下语句: - `db.<COLLECTION_NAME>.count()`:返回集合中文档的数量。 - `db.<COLLECTION_NAME>.count({})`或`db.<COLLECTION_NAME>.find({}).count()`:返回带有条件的文档数量。 要获取指定字段下所有值的去重结果,你可以使用以下语句: - `db.<COLLECTION_NAME>.distinct(keys)`:返回指定字段下所有值的去重结果。 要创建索引、查看索引和删除索引,你可以使用以下语句: - `db.<COLLECTION_NAME>.createIndex(keys, options)`:创建一个指定字段的索引。 - `db.<COLLECTION_NAME>.getIndexes()`:查看集合的所有索引。 - `db.<COLLECTION_NAME>.totalIndexSize()`:查看集合索引的总大小。 - `db.<COLLECTION_NAME>.dropIndexes()`:删除集合的所有索引。 - `db.<COLLECTION_NAME>.dropIndexes("索引名称")`:删除指定的索引。 除了上述操作外,还可以使用聚合函数`count()`来进行聚合操作。 请注意,上述语法中的`<COLLECTION_NAME>`需要根据实际集合名称进行替换。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MongoDB SQL](https://blog.csdn.net/jikui0581/article/details/102579376)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值