-
下载mongodb注意
- 根据业界规则,偶数为稳定版,如3.2.X;奇数为开发版,如3.3.X
- 32bit的mongodb最大只能存放2G的数据,64bit就没有限制
- MongoDB官网安装包下载地址
- MongoDB安装文档
- Ubuntu下安装MongoDB sudo apt-get install mongodb
- 启动MongoDB服务:
sudo mongod
- 创建 /data目录和 /data/db 目录,并指定 读/写/执行 权限
sudo mongod --dbpath=/xxxxx/xxxx
sudo mkdir -p /data/db
sudo chmod 777 /data/db
- 与python交互
# mongo与python交互
# pip install pymongo
from pymongo import *
class MongoDB(object):
def __init__(self):
client = MongoClient('localhost', 27017)
self.db = client.py3
def insert_db(self, doc):
try:
# 构造json对象
doc1={'name':'hr','home':'th'}
doc2={'name':'mc','home':'jc'}
doc=[doc1,doc2]
#调用mongo对象,完成insert
self.db.stu.insert_many(doc)
# self.db.stu.insert_one(doc1)
except Exception as e:
print(e)
def find_db(self, doc):
try:
# doc = self.db.stu.find_one()
cursor = self.db.stu.find({'hometown': '大理'})
for doc in cursor:
print('%s--%s' % (doc['name'], doc['hometown']))
except Exception as e:
print(e)
def update_db(self, doc):
try:
self.db.stu.update_many({'gender':True},{'$set':{'name':'haha'}})
# self.db.stu.update_one({'gender': False}, {'$set': {'name': 'hehe'}})
except Exception as e:
print(e)
def delete_db(self, doc):
try:
self.db.stu.delete_many({'gender':False})
# self.db.stu.delete_one({'gender':False})
except Exception as e:
print(e)