Python3爬虫(十) 数据存储之非关系型数据库MongoDB
Infi-chu:
http://www.cnblogs.com/Infi-chu/
一、非关系型数据库
NoSQL全程是Not Only SQL,非关系型数据库。
NoSQL是基于键值对的,不需要经过SQL层的解析,数据之间没有耦合性,性能非常高。
具体介绍请看(也包括Redis)
http://www.cnblogs.com/Infi-chu/p/8277576.html
二、MongoDB
MongoDB 是由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,内容存储形式类似于JSON。
1.连接MongoDB
import pymongo
client = pymongo.MongoClient(host='127.0.0.1',port=27017) # 默认端口27017
# 另一种写法
client = pymongo.MongoClient('mongodb://127.0.0.1:27017')
2.指定数据库
MongoDB中可以建立很多数据库
目前使用test数据库
import pymongo
client = pymongo.MongoClient(host='127.0.0.1',port=27017)
db = client.test
# 另一种写法
db = client['test']
3.指定集合
MongoDB中每个数据库包含多个集合(collection),他们类似于关系型数据库中的表
目前指定students集合
import pymongo
client = pymongo.MongoClient(host='127.0.0.1',port=27017)
db = client.test
collection = db.students
# 另一种写法
collection = db['students']
4.插入数据
student = {
'id':'1',
'name':'Infi-chu',
'age':'23',
'sex':'male'
}
result = collection.insert(student)
print(result)
【注】
MongoDB中,每一条数据都有一个_id属性来唯一标识
在PyMongo 3.x中官方不推荐使用insert()方法插入数据,
而推荐使用insert_one()和insert_many()方法插入单条或多条数据
student1 = {
'id':'1',
'name':'Infi-chu',
'age':'23',
'sex':'male'
}
student2 = {
'id':'2',
'name':'chu',
'age':'23',
'sex':'male'
}
result1 = collection.insert_one(student1)
result2 = collection.insert_many([student1,student2])
print(result1)
print(result2)
print(result2.inserted_ids)
5.查询数据
使用find_one()和find()方法,前者返回单条结果,后者返回一个生成器对象。
也可以根据ObjectId来查询,此时需要bson库中的objectid
from bson.objectid import ObjectId
result = collection.find_one({'_id':ObjectId('这个集合的id值')})
print(result) # 如果结果不存在则返回None
# 查询年龄大于20的数据
results = collection.find('age':{'$gt':20})
print(results)
for result in results:
print(result) # 通过正则表达式,查看名字以I开头的数据
results = collection.find({'name':{'$regex':'^I.*'}})
MongoDB中的比较符号
符号 含义 例子
$lt 小于 {'age':{'$lt':20}}
$gt 大于 {'age':{'$gt':20}}
$lte 小于等于 {'age':{'$lte':20}}
$gte 大于等于 {'age':{'$gte':20}}
$ne 不等于 {'age':{'$ne':20}}
$in 在范围内 {'age':{'$in':[20,25]}}
$nin 不在范围内 {'age':{'$nin':[20,25]}}
MongoDB中的功能符号
符号 含义 例子 解释
$regex 匹配正则表达式 {'name':{'$regex':'^I.*'}} name以I开头
$exists 属性是否存在 {'name':{'$exists':True}} name属性存在
$type 类型判断 {'age':{'$type':'int'}} age的类型是int
$mod 数字模操作 {'age':{'$mod':[5,0]}} 年龄模5余0
$text 文本查询 {'text':{'$search':'Infi-chu'}} test类型的属性中包含Infi-chu的字符串
$where 高级条件查询 {'where':'obj.fans_count == obj.follows_count'} 自己的粉丝数等于关注数
$set 设置
$inc 增长
6.计数
统计查询结果有多少条数据
count = collection.find().count()
print(count)
7.排序
排序时,直接调用sort()方法,并写明升序或降序
results_up = collection.find().sort('name',pymongo.ASCENDING)
result_down = collection.find().sort('name',pymongo.DESCENDING)
8.偏移
只想去其中某几个元素,可以使用skip()方法偏移几个位置,偏移2,就是忽略前两个元素,从第三个开始
results = collection.find().sort('name',pymongo.ASCENDING).skip(2)
print([result['name'] for result in results])
# 可以使用limit()方法指定要取的结果个数
results = collection.find().sort('name',pymongo.ASCENDING).skip(2).limit(2)
print([result['name'] for result in results])
'''
【注】
在数据库数量非常庞大的时候,不建议使用偏移量去查询,因为会导致内存溢出
'''
# 此时可以使用如下操作
from bson.objectid import ObjectId
collection.find({'_id':{$gt:ObjectId('ID号')}})
9.更新
# 使用update()方法,指定更新的条件和更新后的数据,但是官方并不推荐使用,推荐使用update_one()和update_many()方法
condition = {'name':'Infi-chu'}
student['age']=26
result = collection.update(condition,student)
print(result)
# 使用$set对数据进行更新
result = collection.update(condition,{'$set':student})
print(result)
# update_one()和update_many()
condition = {'name':'Infi-chu'}
student = collection.find_one(condition)
student['age'] = 25
result = collection.update_one(condition,{'$set':student})
print(result)
print(result.matched_count,result.modified_count) # 数据条数,影响的数据条数
10.删除
使用remove()方法,推荐使用delete_one()和delete_many()
# remove()
result = collection.remove({'name':'Infi-chu'})
print(result)
# delete_one() | delete_many()
result = collection.delete_one({'name':'Infi-chu'})
print(result)
print(result.deleted_count)
result = collection.delete_many({'age':{'$lt':30}})
print(result.deleted_count)
11.更多操作
查找后删除、替换、更新
find_one_and_delete()
find_one_and_replace()
find_one_and_update()
操作索引
create_index()
create_indexes()
drop_index()
Python3爬虫(十) 数据存储之非关系型数据库MongoDB的更多相关文章
- 大数据时代的数据存储,非关系型数据库MongoDB
在过去的很长一段时间中,关系型数据库(Relational Database Management System)一直是最主流的数据库解决方案,他运用真实世界中事物与关系来解释数据库中抽象的数据架构. ...
- 大数据时代的数据存储,非关系型数据库MongoDB(一)
原文地址:http://www.cnblogs.com/mokafamily/p/4076954.html 爆炸式发展的NoSQL技术 在过去的很长一段时间中,关系型数据库(Relational Da ...
- 数据存储之非关系型数据库存储----MongoDB存储
MongoDB存储----文档型数据库 利用pymongo连接MongoDB import pymongo client = pymongo.MongoClient(host='localhost', ...
- 第十节:Web爬虫之数据存储与MySQL8.0数据库安装和数据插入
用解析器解析出数据之后,接下来就是存储数据了,保存的形式可以多种多样,最简单的形式是直接保存为文本文件,如 TXT.JSON.csv 另外,还可以保存到数据库中,如关系型数据库MySQL ,非关系型数 ...
- Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用
Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用 原创 2017-04-13 嘟嘟MD 嘟爷java超神学堂 前言 前面几章介绍了一些基础,但都是静 ...
- 数据库基础 非关系型数据库 MongoDB 和 redis
数据库基础 非关系型数据库 MongoDB 和 redis 1 NoSQL简介 访问量增加,频繁的读写 直接访问(硬盘)物理级别的数据,会很慢 ,关系型数据库的压力会很大 所以,需要内存级的读写操作, ...
- 非关系型数据库MongoDB入门
本文分为以下四块简单介绍非关系型数据库MongoDB:1.MongoDB简介.2.MongoDB和关系数据库对比.3.MongoDB基本概念.4.mongo shell的使用以及对MongoDB的增删 ...
- 使用.Net+非关系型数据库MongoDB 实现LBS商家按距离排序_按离我最近排序
.Net MongoDB LBS地理位置定位 开发过程,实现商家按距离排序 前言: 在使用美团点外卖,看电影,找好吃的时候,经常会注意到软件有一个按距离排序,找离我最近的商家,心中有一些疑问,.Net ...
- 非关系型数据库----MongoDB
一.什么是MongoDB? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提 ...
随机推荐
- jsp页面传输到xxAction.java乱码解决
jsp页面传输到xxAction.java乱码解决:jsp:encodeURI(encodeURI("xx"))java:if(!StringUtils.isBlank(belon ...
- 第一次使用Git
这次的作业是关于GIT的,一开始我并不知道GIT是啥,百度了一下才知道Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds ...
- 如何使用Putty登录安装在VirtualBox里的ubuntu
我是在Windows操作系统里用VirtualBox安装了ubuntu操作系统. 在VirtualBox里操作ubuntu的终端不是很方便,比如我想在Windows里复制一些命令到ubuntu的终端执 ...
- 接口测试get请求url拼接函数(python)
get请求地址一般是 协议+域名+端口+路径+参数,除了协议和域名其他均可为空. http(s)://domain:port/path?key1=value1&key2=value2& ...
- 解决Could not get lock /var/cache/apt/archives/lock
在ubuntu apt-get upgrade的时候,遇到: E: Could not get lock /var/cache/ apt/archives/lock - open (11 Resour ...
- log4net 配置完成后发现不能输出日志的解决方法
配置好log4net后发现日志不能输出,打开调试看一下几个属性都是false,(比如isdebugenable =false)这其实是项目的启动时候没有加入一行声明代码导致的,可以在程序的Assemb ...
- 将 form 参数转换为 json 绑定 datagrid 上
$.fn.serializeJson=function(){ var serializeObj={}; var array=this.serializeArray(); var str=this.se ...
- doppia代码支持
stixels_t在stixel.hpp里,存储class stixel的vector
- Python-利用flask模块创建web接口
一.创建一个实现登录的接口 import flask from flask import request #获取参数 # import json #post请求传入json对象时,通过json获取参数 ...
- 论文翻译:XNOR-Net: ImageNet Classification Using BinaryConvolutional Neural Networks
目录 Abstract 1 Introduction 2 Related Work 3 Binary Convolutional Neural Network 3.1 Binary-Weight-Ne ...