Python连接mongodb一般使用pymongo模块

1. pymongo模块的简单使用

### MongoDB存储

## 连接MongoDB
import pymongo # 建立连接对象,2种方法皆可
client = pymongo.MongoClient(host='10.0.0.100', port=27017)
client2 = pymongo.MongoClient('mongodb://10.0.0.100:27017/') # 指定数据库,如无则创建
db = client.test
db2 = client2['test'] # 指定集合,如无则创建
collection = db.students
collection2 = db2['students'] # 插入数据,字典格式
stu = {
'name': 'dmr',
'age': '25',
'score': '80',
'gender': 'frail'
}
stu2 = {
'name': 'asx',
'age': '23',
'score': '81',
'gender': 'frail'
}
stu3 = {
'name': 'scy',
'age': '26',
'score': '66',
'gender': 'male'
}
# result = collection.insert_one(stu)
# result2 = collection.insert_many([stu2, stu3])
# print(result2, result.inserted_id) '''
输出内容:
<pymongo.results.InsertManyResult object at 0x0000000002E9D688> 5e70908ba7233089f696168e
''' # 查询,
# find_one()查询到单个结果返回一个字典,如匹配到多个值则返回匹配到的第一个值
# find()则返回一个生成器对象
## 比较符号
# $lt 小于 {'age': {'$lt': 20}}
# $gt 大于 {'age': {'$gt': 20}}
# $lte 小于等于 {'age': {'$lte': 20}}
# $gte 大于等于 {'age': {'$gte': 20}}
# $ne 不等于 {'age': {'$ne': 20}}
# $in 范围内 {'age': {'$in': [20, 24]}}
# $nin 范围外 {'age': {'$nin': [20, 24]}}
## 功能符号
# $regex 匹配正则表达式 {'name': {'$regex': '^d.*'}} 名字以d开头的
# $exists 属性是否存在 {'name': {'$exists': True}} name属性存在则返回存在的内容
# $type 类型判断 {'age': {'$type': 'int'}} age的类型是否为int,匹配则返回匹配的内容
# $text 匹配正则表达式 {'$text': {'$search': 'dm'}} text类型的属性中包含dm字符串
result = collection.find_one({'name': 'dmr'})
result2 = collection.find({'name': 'dmr'})
print(type(result), type(result2))
print(result, result2)
result = collection.find_one({'gender': 'frail'})
result2 = collection.find({'gender': 'frail'})
print(type(result), type(result2))
print(result, result2)
for item in result2:
print(item) '''
输出内容:
<class 'dict'> <class 'pymongo.cursor.Cursor'>
{'_id': ObjectId('5e708dd5b7d59968e4f1ffef'), 'name': 'dmr', 'age': '25', 'score': '80'} <pymongo.cursor.Cursor object at 0x0000000002E9B7B8>
<class 'dict'> <class 'pymongo.cursor.Cursor'>
{'_id': ObjectId('5e709140e0fe6500676f15c3'), 'name': 'dmr', 'age': '25', 'score': '80', 'gender': 'frail'} <pymongo.cursor.Cursor object at 0x0000000002E9BA90>
{'_id': ObjectId('5e709140e0fe6500676f15c3'), 'name': 'dmr', 'age': '25', 'score': '80', 'gender': 'frail'}
{'_id': ObjectId('5e709140e0fe6500676f15c4'), 'name': 'asx', 'age': '23', 'score': '81', 'gender': 'frail'}
''' ## 计数
count = collection.find({'gender': {'$exists': True}}).count()
print(count) ## 排序
results = collection.find().sort('_id', pymongo.ASCENDING)
print(results)
print([i for i in results]) ## 偏移,skip(2)及忽略前2个元素,limit(2)选择前2个元素,后面的元素忽略
results = collection.find().sort('_id', pymongo.ASCENDING).skip(2)
print(results)
print([i for i in results]) ## 更新
old = {
'name': 'dmr'
}
new = {
'age': 20
}
# 直接替换整条内容
result = collection.update(old, new)
result2 = collection.update_one(old, {'$set': new})
# 仅修改new中键值的内容
result3 = collection.update(old, {'$set': new})
result4 = collection.update_many(old, {'$set': new})
print(result, result2)
r2 = collection.find()
for i in r2:
print(i) ## 删除,remove,delete_one,delete_many
result = collection.remove({'name': 'dmr'})
print(result)
result2 = collection.delete_one({'name': 'dmr'})
print(result2.deleted_count)
result3 = collection.delete_many({'name': 'scy'})
print(result3.deleted_count)
result4 = collection.find()
for i in result4:
print(i)

mongodb存储的基本使用的更多相关文章

  1. MongoDB 存储引擎和数据模型设计

    标签: MongoDB NoSQL MongoDB 存储引擎和数据模型设计 1. 存储引擎 1.1 存储引擎是什么 1.2 MongoDB中的默认存储引擎 2. 数据模型设计 2.1 内嵌和引用 2. ...

  2. 了解mongoDB存储结构

    mongoDB 深入浅出一 了解mongoDB存储结构   MongoDB 深入浅出 数据逻辑结构 1 mongoDB中的文档(document) 相当于 关系性数据库的一条一条的记录 2 colle ...

  3. Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程

    Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...

  4. 使用 MongoDB 存储日志数据

    使用 MongoDB 存储日志数据     线上运行的服务会产生大量的运行及访问日志,日志里会包含一些错误.警告.及用户行为等信息.通常服务会以文本的形式记录日志信息,这样可读性强,方便于日常定位问题 ...

  5. MongoDB存储引擎选择

    MongoDB存储引擎选择 MongoDB存储引擎构架 插件式存储引擎, MongoDB 3.0引入了插件式存储引擎API,为第三方的存储引擎厂商加入MongoDB提供了方便,这一变化无疑参考了MyS ...

  6. MongoDB 存储日志数据

    MongoDB 存储日志数据 https://www.cnblogs.com/nongchaoer/archive/2017/01/11/6274242.html 线上运行的服务会产生大量的运行及访问 ...

  7. 使用 MongoDB 存储商品分类信息

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 这是一篇MongoDB官网上的一篇文章,分析了使用MongoDB存储商品分类信息相比其他数据库的优势,并讲述 ...

  8. 数据存储之非关系型数据库存储----MongoDB存储

    MongoDB存储----文档型数据库 利用pymongo连接MongoDB import pymongo client = pymongo.MongoClient(host='localhost', ...

  9. MongoDB学习笔记(五、MongoDB存储引擎与索引)

    目录: mongoDB存储引擎 mongoDB索引 索引的属性 MongoDB查询优化 mongoDB存储引擎: 目前mongoDB的存储引擎分为三种: 1.WiredTiger存储引擎: a.Con ...

  10. MongoDB 存储引擎选择

    MongoDB存储引擎选择 MongoDB存储引擎构架 插件式存储引擎, MongoDB 3.0引入了插件式存储引擎API,为第三方的存储引擎厂商加入MongoDB提供了方便,这一变化无疑参考了MyS ...

随机推荐

  1. [BZOJ4399]魔法少女LJJ----------线段树进阶

    感谢线段树进阶,给了我重新做人的机会.---------------某不知名OIer,Keen_z Description 题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ ...

  2. 第34篇-解析invokeinterface字节码指令

    与invokevirtual指令类似,当没有对目标方法进行解析时,需要调用LinkResolver::resolve_invoke()函数进行解析,这个函数会调用其它一些函数完成方法的解析,如下图所示 ...

  3. GDI+图形图像技术1

    System.Drawing命名空间提供了对GDI+基本图形功能的访问,其中一些子命名空间中提供了更高级的功能. GDI+由GDI发展而来,是Windows图形显示程序与实际物理设备之间的桥梁. GD ...

  4. C++实现一个SOAP客户端

    目录 简介 实现客户端 准备xml文件 引入库文件 构建请求数据的xml 执行Http协议的POST方法 解析响应数据的xml 测试客户端 附件 简介 在C++中,一般使用gSOAP来实现客户端.服务 ...

  5. jQuery淡入淡出效果

    如果是通过鼠标点击事件来触发动画效果可以使用 $("#button").click(function(){ $("#div").stop().fadeToggl ...

  6. 如何解决Redis缓存雪崩、缓存穿透

    缓存雪崩 数据未加载到缓存中,或者缓存同一时间大面积的失效,从而导致所有请求都去查数据库,导致数据库CPU和内存负载过高,甚至宕机. 比如一个雪崩的简单过程: 1.redis集群大面积故障 2.缓存失 ...

  7. jsonpath语法的基本使用

    jsonpath的安装及使用方式: pip安装: Python3.7\Scripts> pip install jsonpath jsonpath的使用: obj = json.load(ope ...

  8. 一文分析 Android现状及发展前景

    Coding这些年,一直低头"搬砖",好像从未仔细审视过Android的发展现状,亦未好好思考Android的发展前景."低头干活,还要抬头看路",写一篇文章简 ...

  9. CentOS 8.4安装Docker

    前言: Docker 是一个用于开发.传送和运行应用程序的开放平台.Docker 使您能够将应用程序与基础设施分开,以便您可以快速交付软件.使用 Docker,您可以像管理应用程序一样管理基础设施.通 ...

  10. Centos8 Docker部署ElasticSearch集群

    ELK部署 部署ElasticSearch集群 1.拉取镜像及批量生成配置文件 # 拉取镜像 [root@VM-24-9-centos ~]# docker pull elasticsearch:7. ...