45、MongoDB—增、删、改、查
2018-11-07 本文已影响51人
ComfyUI
前言:这章我们说说数据库的常规操作,增、删、改、查。
GitHub:https://github.com/Ewall1106/mall
1、增
- 数据行的增在前面的
mongodb
知识点的讲解中就已经有过涉及了,没错,就是insert
操作。
## 同时创建集合与插入数据
$ use test
## 上面一步switch到了newDatabase
$ db.user.insert({id: 123, name: 'Ewall'})

我们这里新建一个user
集合(表),然后插入一条id
为1
,name
为Ewall
的数据行。
2、删
- 删有很多中操作及匹对规则,具体可以看看mongodb的官网,我这里就只简单的说一种:
$ db.user.remove({"这里填入要删除的条件"})

我们这里在上面插入的数据基础上,又插入了一条id
为2
,name
为pandaCode
的新数据行,然后使用remove
操作进行了删除。
3、改
- 我们试着把名字
Ewall
改为hello world
## 第一个{}里面填要更新的条件,第二个{}是更新的内容
$ db.user.update({},{$set:{}})

4、查
查的话,上面刚刚都已经运用到了,我们这里来细说一下:
(1)查看结合里面插入的所有行
- 我们这里以
user
表为例:
$ db.user.find()
- 格式化:
$ db.user.find().pretty()

(2)查看第一条数据

(3)查找大于、小于、等于等数据操作
## 大于
$ db.user.find({"age": {$gt:3}})
## 小于
$ db.user.find({"age": {$lt:3}})
## 等于
$ db.user.find({"age":{$eq:3}})
## 大于且等于
$ db.user.find({"age": {$gte:3}})
5、小结
至此,本章对于mongdb数据库中一些常规的增删改查操作就讲完了。