当前位置:网站首页>Mongo数据库
Mongo数据库
2022-06-28 10:19:00 【0&1 * 1】
MongoDB简介
1)基于分布式文件存储的开源数据库系统。
2)旨在为WEB应用提供可扩展的高性能数据存储解决方案
3)将数据存储为一个文档,文档类似与Json格式,
{
name:"小明",
age:16,
address: {city:"长沙", country:"china"}
}
五、数据类型
1)类型
[外链图片转存失败(img-YMHwLxQo-1566960480633)(assets/1564387186297.png)]
2)MongoDB进入与退出
[外链图片转存失败(img-jNui0P6B-1566960480633)(assets/1564183945811.png)]
进入 mongo 退出 exit
六、文档操作
1)添加文档(数据)
db.集合名称.insert(document 每一条数据,就是一个document,就是一条json
db.student.insert({name:'xiaoming', age:18})
添加文档时,如果不指定_id参数
MongoDB会为文档分配一个唯一的ObjectId
例: db.student.insert({'_id':1, name:'xiaoming', age:18})
添加多条文档
db.student.insert([
{name:xiaoming', sex:'男', age:16},
{name:’xiaobai', sex:'男', age:18},
{name:’moran‘, sex:’女', age:18},
])
2)查询文档(数据
db.集合名称.find([conditions]) MongoDB的条件语句也十分的强大, 接下来的内容可能会引起不适,请做好心里准备
1.查看集合中全部数据: db.student.find()
2.格式化显示: db.student.find().pretty()
3.查看满足条件的数据: db.student.find({name:'xiaoming'})
3)噩梦条件
操作符 | 描述 |
---|---|
$ne | 不等于 |
$gt | 大于 |
$lt | 小于 |
$gte | 大于等于 |
$lte | 小于等于 |
例子:db.user.find({‘age’: {’$ne’: 45}})
1.and条件
{$and:[{expression1}, {expression1}, ...] }
2.or条件
{$or:[{expression1}, {expression1}, ...] }
3.and和or混用
db.table.find({$or:[{$and:[{sex:'女'}, {age:18}]},{$and:[{sex:'男'}, {age:{$gt:18}}]}]})
4)修改文档(数据)
1.db.集合名称
.update(<query>, <update>, {multi:<boolean>}
2.修改一条数据
db.table.update({sex:'男'}, {age:20})
3.指定属性修改
{ $set: {age:20} }
db.table.update({name:'xiaoming'}, {$set: {age:666, sex: 'xx'}} )
4.更新集合中所有满足条件的文档
{ multi: true }
db.table.update({sex:'男'}, {$set:{sex:'女'}}, { multi:true} )
5)删除文档(数据)
1.db.集合名称
.remove(<query>, {justOne:<boolean>})
2.删除集合中所有的文档
db.table.remove({})
3.删除集合中满足条件的所有文档
db.table.remove({sex: '男'})
4.只删除集合中满足条件的第一条文档
{ justOne: true }
db.table.remove({sex:'男'}, { justOne:true} )
七、Python与MogoDB交互
1)安装
pip install pymongo
2)建立连接
client = pymongo.MongoClient()
3)指定数据库
db = client[数据库名]
4)指定集合
collection=db[集合名]
5)基本使用
1.查找文档
find()
2.添加文档
insert()
3.修改文档
update()
4.删除文档
remove()
6)要点
1.查找
查找一条文档: find_one()
查找所有:find()
2.添加
添加一条文档:insert_one
添加多条:insert_many()
3.删除
删除一条文档:delete_one
删除多条:delete_many()
4.修改
修改一条文档: update_one
修改多条:update_many()
6)将Mongodb饰装成类
import pymongo
# 1.建立连接
client = pymongo.MongoClient()
# 2.指定数据库
db = client["py_49"]
# 3.指定集合
col = db["student"]
# 1.添加文档
# col.insert_one({"name": "lee", "age": 18, "sex": "M"})
# col.insert_many([
# {"name": "tomas", "age": 15, "sex": "M"},
# {"name": "misa", "age": 18, "sex": "F"},
# {"name": "meng", "age": 15, "sex": "F"},
# {"name": "meixi", "age": 16, "sex": "M"},
# {"name": "que", "age": 20, "sex": "M"},
# ])
# 2.查找文档
# res = col.find_one()
# print(res)
# res = col.find()
# print(res) # <pymongo.cursor.Cursor object at 0x7f1213e26668>
# for i in res:
# print(i)
# 3.修改文档
# col.update_one({"name": "misa"}, {"$set": {"age": 15}})
# col.update_many({"age": 15}, {"$set": {"age": 88}})
# 4.删除文档
# col.delete_one({"name": "misa"})
# col.delete_many({"age": 88})
col.delete_many({"$or": [{'name': 'lee'}, {'name': 'meixi'}, {'name': 'que'}]})
res = col.find()
for i in res:
print(i)
边栏推荐
- Katalon框架测试web(二十)自定义关键字以及上传弹窗操作
- Unity loads AssetBundle resources from the server and writes them to local memory, and loads the downloaded and saved AB resources from local memory to the scene
- Transactions proof in appliedzkp zkevm (10)
- Understanding of FTP protocol
- appliedzkp zkevm(9)中的Bytecode Proof
- ECS MySQL query is slow
- Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
- An error is reported when uninstalling Oracle
- DataEase安装升级
- Pop up and push in sequence of stack < difficulty coefficient >
猜你喜欢
Missed the golden three silver four, found a job for 4 months, interviewed 15 companies, and finally got 3 offers, ranking P7+
MySQL cannot be opened. Flash back
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
如何使用 DataAnt 监控 Apache APISIX
mysql数据库概述以及安装过程
Fastposter v2.8.4 release e-commerce poster generator
如图 用sql行转列 图一原表,图二希望转换后
How to use dataant to monitor Apache apisex
Internet of things application case of wireless module transparent transmission technology
etf持仓如何影响现货金价?
随机推荐
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
Discard Tkinter! Simple configuration to quickly generate cool GUI!
解析:去中心化托管解决方案概述
物联网5种无线传输协议特点大汇总
Six fusion positioning technologies in wireless communication application of Internet of things
Understanding of FTP protocol
第五章 树和二叉树
丢弃 Tkinter!简单配置快速生成超酷炫 GUI!
手把手教你处理 JS 逆向之 SVG 映射
Mysql database overview and installation process
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
理想中的接口自动化项目
As shown in the figure, the SQL row is used to convert the original table of Figure 1. Figure 2 wants to convert it
2022 Wu Enda machine learning specialization week 2 practice lab: linear expression
Katalon当中的debug调试
第三章 栈和队列
Installing MySQL database (CentOS) in Linux source code
如何利用k线图做技术分析
Transactions proof in appliedzkp zkevm (10)
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的