1, install python3.5
If Python is not installed, you can install it directly with Yum,
[Python]View PlainCopy
- # But the 2.6 version is installed
- Yum install-y python
Source Installation 3.5
[Python]View PlainCopy
- 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
You need to configure the library before running Python
Echo/usr/local/lib >>/etc/ld.so.conf.d/local.conf
Ldconfig
Run the Demo
Python3--version
Part of the execution process:
[Python]View PlainCopy
- [Email protected]03_SDWM python-3.5. 0]# echo/usr/local/lib >>/etc/ld.so.conf.d/local.conf
- [Email protected]03_SDWM python-3.5. 0]# ldconfig
- [Email protected]03_SDWM python-3.5. 0]#
- [Email protected]03_SDWM python-3.5. 0]#
- [Email protected]03_SDWM python-3.5. 0]# python3--version
- Python 3.5. 0
- [Email protected]03_SDWM python-3.5. 0]#
2, install Pymongo
There are 2 installation methods, namely installing with PIP and installing with Easy_install, which uses installing Witheasy_install reference official article:
Http://api.mongodb.com/python/current/installation.html#installing-with-easy-install,
Install Python Pymongo
[Python]View PlainCopy
- [[email protected]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
- [[email protected]03_SDWM ~]#
3, use the Pymongo operation MongoDB to do some simple operation to the MongoDB library
[Python]View PlainCopy
- #!/usr/bin/env python
- #-*-Coding:utf-8-*-
- Import Pymongo
- Import datetime
- Def get_db ():
- # Establish a connection
- Client = Pymongo. Mongoclient (host="10.244.25.180", port=27017)
- db = client[' example ')
- #或者 db = Client.example
- return DB
- def get_collection (db):
- # Select Collection (Mongo in collection and database are created by inertia)
- coll = db[' informations ']
- print db.collection_names ()
- return Coll
- def insert_one_doc (db):
- # Insert a document
- coll = db[' informations ']
- Information = {"name": "Quyang", "age": " +"}
- information_id = Coll.insert (information)
- Print information_id
- def insert_multi_docs (db):
- # Insert documents in bulk, insert an array
- coll = db[' informations ']
- Information = [{"name": " xiaoming", "age": "+" }, {"name": "Xiaoqiang", "age": "24"}]
- information_id = Coll.insert (information)
- Print information_id
- def get_one_doc (db):
- # There's just one return, no return none
- coll = db[' informations ']
- print Coll.find_one () # returns the first record
- Print Coll.find_one ({"name": "Quyang"})
- Print Coll.find_one ({"name": "None"})
- def get_one_by_id (db):
- # Find a doc with Objectid
- coll = db[' informations ']
- obj = Coll.find_one ()
- obj_id = obj["_id"]
- print "_id for Objectid type, obj_id:" + str (obj_id)
- Print Coll.find_one ({"_id": obj_id})
- # Note that the obj_id here is an object, not a str, and cannot be found using the STR type as the _id value.
- print "_id for str type"
- Print Coll.find_one ({"_id": Str (OBJ_ID)})
- # The Objectid method can be used to turn str into Objectid type
- From bson.objectid import objectid
- Print "_id converted to Objectid type"
- Print Coll.find_one ({"_id": ObjectId (str (obj_id))})
- def get_many_docs (db):
- # MONGO provides a method for filtering lookups, which can be used to get data sets by various criteria filters, and to count, sort, and process data .
- coll = db[' informations ']
- #ASCENDING = 1 ascending; descending = 1 descending; default is ascending
- for item in Coll.find (). Sort ("age", Pymongo. Descending):
- Print Item
- Count = Coll.count ()
- print "All data in collection%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", "py" Mongo "],
- "Date": Datetime.datetime.utcnow ()}
- # Insert Record
- My_collection.insert (POST)
- Insert_one_doc (DB)
- # Conditional Query
- Print My_collection.find_one ({"x": "Ten"})
- # Query All the data in the table
- 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 uses Pymongo to manipulate the MongoDB library