mongodb存储的基本使用
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存储的基本使用的更多相关文章
- MongoDB 存储引擎和数据模型设计
标签: MongoDB NoSQL MongoDB 存储引擎和数据模型设计 1. 存储引擎 1.1 存储引擎是什么 1.2 MongoDB中的默认存储引擎 2. 数据模型设计 2.1 内嵌和引用 2. ...
- 了解mongoDB存储结构
mongoDB 深入浅出一 了解mongoDB存储结构 MongoDB 深入浅出 数据逻辑结构 1 mongoDB中的文档(document) 相当于 关系性数据库的一条一条的记录 2 colle ...
- Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...
- 使用 MongoDB 存储日志数据
使用 MongoDB 存储日志数据 线上运行的服务会产生大量的运行及访问日志,日志里会包含一些错误.警告.及用户行为等信息.通常服务会以文本的形式记录日志信息,这样可读性强,方便于日常定位问题 ...
- MongoDB存储引擎选择
MongoDB存储引擎选择 MongoDB存储引擎构架 插件式存储引擎, MongoDB 3.0引入了插件式存储引擎API,为第三方的存储引擎厂商加入MongoDB提供了方便,这一变化无疑参考了MyS ...
- MongoDB 存储日志数据
MongoDB 存储日志数据 https://www.cnblogs.com/nongchaoer/archive/2017/01/11/6274242.html 线上运行的服务会产生大量的运行及访问 ...
- 使用 MongoDB 存储商品分类信息
此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 这是一篇MongoDB官网上的一篇文章,分析了使用MongoDB存储商品分类信息相比其他数据库的优势,并讲述 ...
- 数据存储之非关系型数据库存储----MongoDB存储
MongoDB存储----文档型数据库 利用pymongo连接MongoDB import pymongo client = pymongo.MongoClient(host='localhost', ...
- MongoDB学习笔记(五、MongoDB存储引擎与索引)
目录: mongoDB存储引擎 mongoDB索引 索引的属性 MongoDB查询优化 mongoDB存储引擎: 目前mongoDB的存储引擎分为三种: 1.WiredTiger存储引擎: a.Con ...
- MongoDB 存储引擎选择
MongoDB存储引擎选择 MongoDB存储引擎构架 插件式存储引擎, MongoDB 3.0引入了插件式存储引擎API,为第三方的存储引擎厂商加入MongoDB提供了方便,这一变化无疑参考了MyS ...
随机推荐
- CodeForces-1076E Vasya and a Tree
CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in verte ...
- 【BZOJ-2199】奶牛议会
链接: BZOJ-2199 题意: 给出 \(n(1\leq n\leq 1000)\) 个点,\(m(1\leq m\leq 4000)\) 个形如:"点 \(a\) 取 \(ca\) 或 ...
- Luogu P1297 [国家集训队]单选错位 | 概率与期望
题目链接 题解: 单独考虑每一道题目对答案的贡献. 设$g_i$表示gx在第$i$道题目的答案是否正确(1表示正确,0表示不正确),则$P(g_i=1)$表示gx在第$i$道题目的答案正确的概率. 我 ...
- Windows7下面手把手教你安装Django - Hongten
我所使用的操作系统是Windows7,内存是2G 在搜索了一些资料发现,对于Django的安装,详细的真的很少,都说的很简化,然而,这篇blog可以手把手教你成功安装Django 对于Django的详 ...
- POJ 2584 T-Shirt Gumbo(二分图最大匹配)
题意: 有五种衣服尺码:S,M,L,X,T N个人,每个人都有一个可以穿的衣服尺码的范围,例:SX,意思是可以穿S,M,L,X的衣服. 给出五种尺码的衣服各有多少件. 如果可以满足所有人的要求,输出 ...
- Centos 系统常用编译环境
centos编译环境配置 yum install -y autoconf make automake gcc gcc-c++
- Redis监控调研
1 调研目的 主要的目的是想调研各大云平台有关Redis监控功能的实现,但是最后我发现各大云平台提供的监控功能都比较基础,比如我想看诸如访问频率较高的HotKey.占用内存较大的Bigkey等指标,它 ...
- mysql登录遇到ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
执行mysql -uroot -p,出现如下问题 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using pass ...
- Git撤销、回滚操作
git的工作流 工作区:即自己当前分支所修改的代码,git add xx 之前的!不包括 git add xx 和 git commit xxx 之后的. 暂存区:已经 git add xxx 进去, ...
- MAC电脑如何将常规视频中音频提取出来(转换格式并调整采样频率),并利用讯飞语音识别文字
1.下载好相关视频 2.选中需要提取视频,鼠标右键找到「编码所选视频文件」 3.设置中,下拉选择「仅音频」,点击继续 4.找到已提取成功的音频,鼠标右键或快捷键「command + I」,显示简介.默 ...