码迷,mamicode.com
首页 > 数据库 > 详细

爬虫文件存储-2:MongoDB

时间:2019-01-29 20:38:11      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:存在   没有   idt   示例   class   根据   需要   文本查询   obj   

1.连接MongoDB

连接 MongoDB 我们需要使用 PyMongo 库里面的 MongoClient,一般来说传入 MongoDB 的 IP 及端口即可,第一个参数为地址 host,第二个参数为端口 port,端口如果不传默认是 27017。

import pymongo
client = pymongo.MongoClient(host=localhost, port=27017)
# client = MongoClient(‘mongodb://localhost:27017/‘)

2.指定数据库

import pymongo
client = pymongo.MongoClient(host=localhost, port=27017)
# client = MongoClient(‘mongodb://localhost:27017/‘)
db = client.test
# db = client[‘test‘]

3.指定集合

MongoDB 的每个数据库又包含了许多集合 Collection,也就类似与关系型数据库中的表

import pymongo
client = pymongo.MongoClient(host=localhost, port=27017)
# client = MongoClient(‘mongodb://localhost:27017/‘)
db = client.test
# db = client[‘test‘]
collection = db.students
# collection = db[‘students‘]

4.插入数据

在 MongoDB 中,每条数据其实都有一个 _id 属性来唯一标识,如果没有显式指明 _id,MongoDB 会自动产生一个 ObjectId 类型的 _id 属性。insert() 方法会在执行后返回的 _id 值。

也可以同时插入多条数据,只需要以列表形式传递即可,返回的结果是对应的 _id 的集合。

import pymongo
client = pymongo.MongoClient(host=localhost, port=27017)
# client = MongoClient(‘mongodb://localhost:27017/‘)
db = client.test
# db = client[‘test‘]
collection = db.students
# collection = db[‘students‘]

# 插入单条数据
student = {
    id: 20170101,
    name: Jordan,
    age: 20,
    gender: male
}

result = collection.insert_one(student)
print(result)
print(result.inserted_id) # 返回的是InsertOneResult 对象,我们可以调用其 inserted_id 属性获取 _id
"""
运行结果:
<pymongo.results.InsertOneResult object at 0x10d68b558>
5932ab0f15c2606f0c1cf6c5
"""

# 插入多条数据
student1 = {
    id: 20170101,
    name: Jordan,
    age: 20,
    gender: male
}

student2 = {
    id: 20170202,
    name: Mike,
    age: 21,
    gender: male
}

result = collection.insert_many([student1, student2])
print(result)
print(result.inserted_ids) # insert_many() 方法返回的类型是 InsertManyResult,调用inserted_ids 属性可以获取插入数据的 _id 列表
"""
运行结果:
<pymongo.results.InsertManyResult object at 0x101dea558>
[ObjectId(‘5932abf415c2607083d3b2ac‘), ObjectId(‘5932abf415c2607083d3b2ad‘)]

"""

5.查询数据

可以利用 find_one() 或 find() 方法进行查询,find_one() 查询得到是单个结果,find() 则返回一个生成器对象。

import pymongo
client = pymongo.MongoClient(host=localhost, port=27017)
# client = MongoClient(‘mongodb://localhost:27017/‘)
db = client.test
# db = client[‘test‘]
collection = db.students
# collection = db[‘students‘]

result = collection.find_one({name: Mike})
print(type(result)) # 查询 name 为 Mike 的数据,它的返回结果是字典类型
print(result) # 多了一个 _id 属性,这就是 MongoDB 在插入的过程中自动添加的
"""
<class ‘dict‘>
{‘_id‘: ObjectId(‘5c502697e0b72c0d90eeee22‘), ‘id‘: ‘20170202‘, ‘name‘: ‘Mike‘, ‘age‘: 21, ‘gender‘: ‘male‘}
"""

# 也可以直接根据 ObjectId 来查询,这里需要使用 bson 库里面的 ObjectId
"""
from bson.objectid import ObjectId

result = collection.find_one({‘_id‘: ObjectId(‘593278c115c2602667ec6bae‘)})
print(result)

"""

# 多条数据的查询,使用 find() 方法
results = collection.find({age: 20})
print(results)
for result in results:
    print(result) # 返回结果是 Cursor 类型,相当于一个生成器,我们需要遍历取到所有的结果,每一个结果都是字典类型

# 要查询年龄大于 20 的数据
results = collection.find({age: {$gt: 20}}) # 查询的条件键值是一个字典,其键名为比较符号 $gt,意思是大于,键值为 20

符号

含义

示例

$lt

小于

{‘age‘: {‘$lt‘: 20}}

$gt

大于

{‘age‘: {‘$gt‘: 20}}

$lte

小于等于

{‘age‘: {‘$lte‘: 20}}

$gte

大于等于

{‘age‘: {‘$gte‘: 20}}

$ne

不等于

{‘age‘: {‘$ne‘: 20}}

$in

在范围内

{‘age‘: {‘$in‘: [20, 23]}}

$nin

不在范围内

{‘age‘: {‘$nin‘: [20, 23]}}


# 还可以进行正则匹配查询,例如查询名字以 M 开头的学生数据
results = collection.find({name: {$regex: ^M.*}}) # 使用了 $regex 来指定正则匹配,^M.* 代表以 M 开头的正则表达式s

符号

含义

示例

示例含义

$regex

匹配正则

{‘name‘: {‘$regex‘: ‘^M.*‘}}

name 以 M开头

$exists

属性是否存在

{‘name‘: {‘$exists‘: True}}

name 属性存在

$type

类型判断

{‘age‘: {‘$type‘: ‘int‘}}

age 的类型为 int

$mod

数字模操作

{‘age‘: {‘$mod‘: [5, 0]}}

年龄模 5 余 0

$text

文本查询

{‘$text‘: {‘$search‘: ‘Mike‘}}

text 类型的属性中包含 Mike 字符串

$where

高级条件查询

{‘$where‘: ‘obj.fans_count == obj.follows_count‘}

自身粉丝数等于关注数

 

 

爬虫文件存储-2:MongoDB

标签:存在   没有   idt   示例   class   根据   需要   文本查询   obj   

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/10335391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!