pymongo的使用
一、连接数据库
import pymongo
if __name__ == "__main__":
# 创建连接
client = pymongo.MongoClient(host='localhost', port=27017)
# 指定连接的数据库test
db = client.test
# 指定的集合(类似表)
collection = db.scrapy_items
# 利用集合来操作数据库
# result = collection.find()
# for i in result:
# print(i)
result1 = collection.find_one({'title': '运维学python之爬虫基础篇(一)开篇'})
print(result1)
二、把连接数据库的封装到一个包下
1、连接数据库
import pymongo class PymongoDb(object): def __init__(self): self.host = 'localhost' self.port = 27017 def collection(self): # 创建连接 client = pymongo.MongoClient(host=self.host, port=self.port) # 使用哪个数据库 db = client.test # 指定表 collection = db.mongo_item return collection
三、操作数据
1、插入单条数据
from day10.db.pymongo_db import PymongoDb if __name__ == '__main__': collection = PymongoDb().collection() student = { 'id': '20170101', 'name': '哈哈', 'age': 30, 'gender': '女' } result = collection.insert(student) print(result)
2、插入多条数据
from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() student1 = { 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male' } student2 = { 'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male' } result = collection.insert([student1, student2]) print(result)
3、使用
insert_one
插入数据from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() student = { 'name': '张三', 'age': 20, 'gender': '男' } result = collection.insert_one(student) print(result)
4、使用
insert_many
插入多条数据from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() 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.inserted_ids)
5、查询单条数据
from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() result = collection.find_one({'name': '张三'}) print(result)
6、
find({})
查询数据from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() result = collection.find({'age': 20}) print(result) for i in result: print(i)
7、条件查询
- $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]}}
from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() result = collection.find({'age': {'$gt': 20}}) for i in result: print(i)
8、关于计数
from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() result = collection.find().count() print(result) result1 = collection.find({'age': {'$gt': 20}}).count() print(result1)
9、排序操作
import pymongo from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() result = collection.find().sort('age', pymongo.ASCENDING) for i in result: print(i)
10、修改数据
from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() result = collection.find_one({'name': '张三'}) result['age'] = 30 result1 = collection.update({'name': '张三'}, result) print(result1)
11、删除数据
from day10.db.pymongo_db import PymongoDb if __name__ == "__main__": collection = PymongoDb().collection() result = collection.remove({'name': '张三'}) print(result)