当前位置:网站首页>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)
边栏推荐
猜你喜欢

sentinel

无线模块透明传输技术的物联网应用案例

Caffeine cache, the king of cache, has stronger performance than guava

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"

理想中的接口自动化项目

Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface

What is the best way to learn machine learning

第六天 脚本与动画系统

港伦敦金行情走势图所隐藏的信息

物联网5种无线传输协议特点大汇总
随机推荐
如何利用k线图做技术分析
MySQL general binary installation method
Generate token
Summary of characteristics of five wireless transmission protocols of Internet of things
The introduction of flink-sql-mysql-cdc-2.2.1 has solved many dependency conflicts?
【OpenCV 例程200篇】213. 绘制圆形
bye! IE browser, this route edge continues to go on for IE
Hystrix 部署
Pop up and push in sequence of stack < difficulty coefficient >
[Unity]EBUSY: resource busy or locked
An error is reported when uninstalling Oracle
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
How to use dataant to monitor Apache apisex
[Unity][ECS]学习笔记(三)
2D code generator for openharmony application development
Fabric.js 笔刷到底怎么用?
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
【功能建议】多个工作空间启动时选择某个空间
解决表单action属性传参时值为null的问题
[NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting