上一篇文章已经运用了Pymongo这个包,本篇文章详细介绍下Pymongo的使用
PyMongo是Mongodb的Python接口开发包,是使用python和Mongodb的推荐方式。
进入虚拟环境
sudo pip install pymongo
或源码安装
python setup.py
import pymongo
#或者
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
# 或者
client = MongoClient('mongodb://localhost:27017/')
db = client.test_database
# 或者
db = client['test-database']
collection = db.test_collection
# 或者
collection = db['test-collection']
>>> import datetime
>>> post = {"author": "Mike",
"text": "My first blog post!",
"tags": ["mongodb", "python", "pymongo"],
"date": datetime.datetime.utcnow()}
>>> posts = db.posts
>>> post_id = posts.insert_one(post).inserted_id
>>> post_id
ObjectId('...')
>>> posts.find_one()
{u'date': datetime.datetime(...), u'text': u'My first blog post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'mongodb', u'python', u'pymongo']}
>>> posts.find_one({"author": "Mike"})
{u'date': datetime.datetime(...), u'text': u'My first blog post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'mongodb', u'python', u'pymongo']}
>>> posts.find_one({"author": "Eliot"})
>>>
>>> post_id ObjectId(...)
>>> posts.find_one({"_id": post_id})
{u'date': datetime.datetime(...), u'text': u'My first blog post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'mongodb', u'python', u'pymongo']}
>>> post_id_as_str = str(post_id)
>>> posts.find_one({"_id": post_id_as_str}) # No result
>>>
from bson.objectid import ObjectId
# The web framework gets post_id from the URL and passes it as a string def get(post_id):
# Convert from string to ObjectId:
document = client.db.collection.find_one({'_id': ObjectId(post_id)})
>>> new_posts = [{"author": "Mike",
"text": "Another post!",
"tags": ["bulk", "insert"],
"date": datetime.datetime(2009, 11, 12, 11, 14)},
{"author": "Eliot",
"title": "MongoDB is fun",
"text": "and pretty easy too!",
"date": datetime.datetime(2009, 11, 10, 10, 45)}]
>>> result = posts.insert_many(new_posts)
>>> result.inserted_ids
[ObjectId('...'), ObjectId('...')]
>>> for post in posts.find():
... post
...
{u'date': datetime.datetime(...), u'text': u'My first blog post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'mongodb', u'python', u'pymongo']}
{u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'bulk', u'insert']}
{u'date': datetime.datetime(2009, 11, 10, 10, 45), u'text': u'and pretty easy too!', u'_id': ObjectId('...'), u'author': u'Eliot', u'title': u'MongoDB is fun'}
#查找多个文档2
cur=stu.find()
cur.next()
posts.count()
#满足某种查找条件的数据条数:
posts.find({"author": "Mike"}).count()
#比如说时间范围
>>> d = datetime.datetime(2009, 11, 12, 12)
>>> for post in posts.find({"date": {"$lt": d}}).sort("author"):
... print post
{u'date': datetime.datetime(2009, 11, 10, 10, 45), u'text': u'and pretty easy too!', u'_id': ObjectId('...'), u'author': u'Eliot', u'title': u'MongoDB is fun'}
{u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'bulk', u'insert']}
root:只在admin数据库中可用,超级账号,超级权限 Read:允许用户读取指定数据库 readWrite:允许用户读写指定数据库
use admin
db.createUser({
user:'admin',
pwd:'123',
roles:[{role:'root',db:'admin'}]
})
mongo进入操作界面
use admin # 指定数据库
db.addUser(“用户名”, “密码”) 添加用户 # 可以使用,已经提示过时
db.createUser(“用户名”, “密码”) 添加用户
sudo vi /etc/mongod.conf
security:
authorization: enabled
sudo service mongod stop
sudo service mongod start
mongo -u 'admin' -p '123' --authenticationDatabase 'admin'
use test1
show users
db.createUser({
user:'t1',
pwd:'123',
roles:[{role:'readWrite',db:'test1'}]
})
mongo -u t1 -p 123 --authenticationDatabase test1
db.updateUser('t1',{pwd:'456'})
mkdir t1 mkdir t2
mongod --bind_ip 192.168.10.110 --port 27018 --dbpath ~/Desktop/t1 -->replSet rs0 mongod --bind_ip 192.168.10.111 --port 27019 --dbpath ~/Desktop/t2 -->replSet rs0
mongo --host 192.168.10.110 --port 27018
rs.initiate()
rs.status()
rs.add('192.168.10.111:27019')
mongo --host 192.168.10.111 --port 27019
use test1 for(i=0;i<10;i++){db.t1.insert({_id:i})} db.t1.find()
rs.slaveOk() db.t1.find()
rs.remove('192.168.10.111:27019') 关闭主服务器后,再重新启动,会发现原来的从服务器变为了主服务器,新启动的服务器(原来的从服务器)变为了从服务器
mongodump -h dbhost -d dbname -o dbdirectory
#例
sudo mkdir test1bak
sudo mongodump -h 192.168.10.110:27017 -d test1 -o ~/flex/test1bak
mongorestore -h dbhost -d dbname --dir dbdirectory
#例
mongorestore -h 192.168.10.110:27017 -d test2 --dir ~/flex/test1bak/test1
#爬取英雄联盟英雄信息详情,并存储
#-*- coding:utf-8 -*-
import pymongo
import requests
from bs4 import BeautifulSoup
#建立于MongoClient 的连接
client = pymongo.MongoClient('localhost',27017)
#得到数据库
hero = client['hero']
#得到一个数据集合
sheet_tab = hero['sheet_tab']
url = 'http://lol.duowan.com/hero/'
req = requests.get(url)
soup = BeautifulSoup(req.text,'html.parser')
links = soup.find(id="champion_list").find_all('a')
for link in links:
link = link['href']
requ = requests.get(link)
sop = BeautifulSoup(requ.text,'html.parser')
data = {
'title' : sop.find('h2',class_="hero-title").get_text(),
'name' : sop.find('h1',class_="hero-name").get_text(),
'tags' : sop.find('div',class_="hero-box ext-attr").find_all('span')[1].get_text(),
'story' : sop.find('div',class_="hero-popup").find_all('p')[0].get_text(),
}
sheet_tab.insert_one(data)
通过Robo 3T可视化工具我们可以看到抓取到了137条数据,并且已存储到mongodb中
#-*- coding:utf-8 -*-
import pymongo
def system():
print('◆您将进入数据库管理系统,数据无价、谨慎操作!◆')
print('◇1:查看数据◇')
print('◇2:增加数据◇')
print('◇3:修改数据◇')
print('◇4:删除数据◇')
print('◇5:搜索数据◇')
print('●6:退出数据库管理系统●')
# 建立与mongodb的连接
client = pymongo.MongoClient('localhost', 27017)
# 得到数据库
stu = client['stu']
# 得到一个数据集合
message = stu['message']
while True:
order = int(raw_input('请输入相关指令:'))
if order==1:
exit = message.count()
if exit==0:
print('抱歉,数据库中目前没有相关数据!')
else:
for data in message.find():
content = data['name']+data['age']+data['sex']
print(content)
elif order ==2:
name = raw_input('请输入学生姓名:')
age = raw_input('请输入学生年龄:')
sex = raw_input('请输入学生性别(男/女):')
data = {
'name':name,
'age':age,
'sex':sex,
}
message.insert_one(data)
print ('添加成功!')
elif order == 3:
name = raw_input('请输入要修改的学生姓名:')
exit = message.count({'name': name})
if exit != 0:
age = raw_input('请输入修改后的学生年龄:')
message.update({'name':name},{'$set':{'age':age}})
print('修改成功')
else:
print '抱歉,数据库中没有这个学生的信息!'
elif order == 4:
name = raw_input('请输入要删除的学生姓名:')
exit = message.count({'name': name})
if exit != 0:
message.remove({'name':name})
print('删除成功')
else:
print '抱歉,数据库中没有这个学生的信息!'
elif order == 5:
name = raw_input('请输入要查询的学生姓名:')
exit = message.count({'name':name})
if exit!=0:
data = message.find_one({'name':name})
content = data['name']+data['age']+data['sex']
print content
else:
print '抱歉,数据库中没有这个学生的信息!'
elif order == 6:
print('感谢您的使用!')
break
else:
print('您的输入有误,请输入有效指令(1/2/3/4/5)')
if __name__ == '__main__':
system()
测试结果