Python 使用pymongo操作mongodb庫
阿新 • • 發佈:2017-11-04
演示 clear bject conf dconf www. pda des tails
1,安裝python3.5
如果python還沒有安裝,可以直接用yum安裝,
[python] view plain copy
- # 不過安裝的是2.6 version
- yum install -y python
源碼安裝3.5
[python] view plain copy
- wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
- tar -xvf Python-3.5.0.tgz
- cd Python-3.5.0
- ./configure --prefix=/usr/local--enable-shared
- make
- make install
- ln -s /usr/local/bin/python3 /usr/bin/python3
運行Python之前需要配置庫
echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
ldconfig
運行演示
python3 --version
部分執行過程:
[python] view plain copy
- [root@03_sdwm Python-3.5.0]# echo/usr/local/lib >> /etc/ld.so.conf.d/local.conf
- [root@03_sdwm Python-3.5.0]# ldconfig
- [root@03_sdwm Python-3.5.0]#
- [root@03_sdwm Python-3.5.0]#
- [root@03_sdwm Python-3.5.0]# python3--version
- Python 3.5.0
- [root@03_sdwm Python-3.5.0]#
2,安裝pymongo
安裝方法有2種,分別是Installing with pip和Installing with easy_install,這裏采用Installing witheasy_install參考官方文章:
http://api.mongodb.com/python/current/installation.html#installing-with-easy-install,
安裝python pymongo
[python] view plain copy
- [root@03_sdwm ~]# python3 -m easy_install pymongo
- Searching for pymongo
- Reading http://pypi.python.org/simple/pymongo/
- Best match: pymongo 3.4.0
- Downloading https://pypi.python.org/packages/82/26/f45f95841de5164c48e2e03aff7f0702e22cef2336238d212d8f93e91ea8/pymongo-3.4.0.tar.gz#md5=aa77f88e51e281c9f328cea701bb6f3e
- Processing pymongo-3.4.0.tar.gz
- Running pymongo-3.4.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ZZv1Ig/pymongo-3.4.0/egg-dist-tmp-LRDmoy
- zip_safe flag not set; analyzing archive contents...
- Adding pymongo 3.4.0 to easy-install.pth file
- Installed /usr/lib/python2.6/site-packages/pymongo-3.4.0-py2.6-linux-x86_64.egg
- Processing dependencies for pymongo
- Finished processing dependencies for pymongo
- [root@03_sdwm ~]#
3,使用pymongo操作mongodb
進行一些簡單的對mongodb庫的操作[python] view plain copy
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import pymongo
- import datetime
- def get_db():
- # 建立連接
- client = pymongo.MongoClient(host="10.244.25.180", port=27017)
- db = client[‘example‘]
- #或者 db = client.example
- return db
- def get_collection(db):
- # 選擇集合(mongo中collection和database都是延時創建的)
- coll = db[‘informations‘]
- print db.collection_names()
- return coll
- def insert_one_doc(db):
- # 插入一個document
- coll = db[‘informations‘]
- information = {"name": "quyang", "age": "25"}
- information_id = coll.insert(information)
- print information_id
- def insert_multi_docs(db):
- # 批量插入documents,插入一個數組
- coll = db[‘informations‘]
- information = [{"name": "xiaoming", "age": "25"}, {"name": "xiaoqiang", "age": "24"}]
- information_id = coll.insert(information)
- print information_id
- def get_one_doc(db):
- # 有就返回一個,沒有就返回None
- coll = db[‘informations‘]
- print coll.find_one() # 返回第一條記錄
- print coll.find_one({"name": "quyang"})
- print coll.find_one({"name": "none"})
- def get_one_by_id(db):
- # 通過objectid來查找一個doc
- coll = db[‘informations‘]
- obj = coll.find_one()
- obj_id = obj["_id"]
- print "_id 為ObjectId類型,obj_id:" + str(obj_id)
- print coll.find_one({"_id": obj_id})
- # 需要註意這裏的obj_id是一個對象,不是一個str,使用str類型作為_id的值無法找到記錄
- print "_id 為str類型 "
- print coll.find_one({"_id": str(obj_id)})
- # 可以通過ObjectId方法把str轉成ObjectId類型
- from bson.objectid import ObjectId
- print "_id 轉換成ObjectId類型"
- print coll.find_one({"_id": ObjectId(str(obj_id))})
- def get_many_docs(db):
- # mongo中提供了過濾查找的方法,可以通過各種條件篩選來獲取數據集,還可以對數據進行計數,排序等處理
- coll = db[‘informations‘]
- #ASCENDING = 1 升序;DESCENDING = -1降序;default is ASCENDING
- for item in coll.find().sort("age", pymongo.DESCENDING):
- print item
- count = coll.count()
- print "集合中所有數據 %s個" % int(count)
- #條件查詢
- count = coll.find({"name":"quyang"}).count()
- print "quyang: %s"%count
- def clear_all_datas(db):
- #清空一個集合中的所有數據
- db["informations"].remove()
- if __name__ == ‘__main__‘:
- db = get_db()
- my_collection = get_collection(db)
- post = {"author": "Mike", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"],
- "date": datetime.datetime.utcnow()}
- # 插入記錄
- my_collection.insert(post)
- insert_one_doc(db)
- # 條件查詢
- print my_collection.find_one({"x": "10"})
- # 查詢表中所有的數據
- for iii in my_collection.find():
- print iii
- print my_collection.count()
- my_collection.update({"author": "Mike"},
- {"author": "quyang", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"],
- "date": datetime.datetime.utcnow()})
- for jjj in my_collection.find():
- print jjj
- get_one_doc(db)
- get_one_by_id(db)
- get_many_docs(db)
- # clear_all_datas(db)
Python 使用pymongo操作mongodb庫