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 ...
随机推荐
- CSDN app分析
项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) (北京航空航天大学 - 计算机学院) 这个作业的要求在哪里 个人博客作业-软件案例分析 我的教学班级 005 说说csd ...
- 了解 js 堆内存 、栈内存 。
js中的堆内存与栈内存 在js引擎中对变量的存储主要有两种位置,堆内存和栈内存. 和java中对内存的处理类似,栈内存主要用于存储各种基本类型的变量,包括Boolean.Number.String.U ...
- sql_exporter的使用
sql_exporter的使用 一.背景 二.sql-exporter的使用 1.下载 2.配置文件 1.sql_exporter.yml 2.collectors 目录中的配置文件 1.collec ...
- elasticsearch入门(简单的crud操作)
记录一下,elasticsearch从创建索引到插入数据的一个crud操作. 一.创建索引 curl -XPUT "http://192.168.99.1:9200/productindex ...
- 小白学习C语言必背的18个经典程序
1./*输出9*9口诀.共9行9列,i控制行,j控制列.*/ #include "stdio.h" main() {int i,j,result; for (i=1;i<10 ...
- HITS算法简介
1.算法名称 超文本敏感标题搜索 (Hyperlink-Induced Topic Search) 2.算法背景 HITS 算法是由康奈尔大学的Jon Kleinberg 博士于1997 年首先提出的 ...
- 使用spire.doc导出支持编辑Latex公式的标准格式word
背景 之前有的教辅标注需求,在导出题库的时候希望顺便导出可以查看word,方便线下预览成品效果,因为只是用来预览并且为了沿用前端的样式,当时方案就是直接生成html,写个word的文件头,这样就可以用 ...
- Android WebView 实现文件选择、拍照、录制视频、录音
原文地址:Android WebView 实现文件选择.拍照.录制视频.录音 | Stars-One的杂货小窝 Android中的WebView如果不进行相应的设置,H5页面的上传按钮是无法触发And ...
- 【java+selenium3】JavaScript的调用执行 (十)
JavaScript的调用 在web自动化操作页面的时候,有些特殊的情况selenium的api无法完成,需要通过执行一段js来实现的DOM操作: //执行方式 JavascriptExecutor ...
- 子查询 & 联合查询
子查询 嵌套在其他语句内部的select语句称为子查询或内查询,外层的语句可以是insert.update.delete.select等,一般select作为外层语句较多.外面如果为select语句, ...