Python uses Pymongo to manipulate the MongoDB library

Source: Internet
Author: User
Tags datetime

1, install python3.5

If Python is not installed, you can install it directly with Yum,

[Python]View PlainCopy
    1. # But the 2.6 version is installed
    2. Yum install-y python

Source Installation 3.5

[Python]View PlainCopy
    1. wget https://www.python.org/ftp/python/3.5. 0/python-3.5. 0.tgz
    2. TAR-XVF python-3.5. 0.tgz
    3. CD python-3.5. 0
    4. ./configure--prefix=/usr/local--enable-shared
    5. Make
    6. Make install
    7. 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
  1. [Email protected]03_SDWM python-3.5. 0]# echo/usr/local/lib >>/etc/ld.so.conf.d/local.conf
  2. [Email protected]03_SDWM python-3.5. 0]# ldconfig
  3. [Email protected]03_SDWM python-3.5. 0]#
  4. [Email protected]03_SDWM python-3.5. 0]#
  5. [Email protected]03_SDWM python-3.5. 0]# python3--version
  6. Python 3.5. 0
  7. [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
  1. [[email protected]03_SDWM ~]# python3-m easy_install Pymongo
  2. Searching for Pymongo
  3. Reading http://pypi.python.org/simple/pymongo/
  4. Best Match:pymongo 3.4. 0
  5. Downloading https://pypi.python.org/packages/82/26/ f45f95841de5164c48e2e03aff7f0702e22cef2336238d212d8f93e91ea8/pymongo-3.4. 0.tar.gz#md5 =aa77f88e51e281c9f328cea701bb6f3e
  6. Processing pymongo-3.4. 0.tar.gz
  7. Running pymongo-3.4. 0/setup.py-q Bdist_egg--dist-dir/tmp/easy_install-zzv1ig/pymongo-3.4. 0/egg-dist-tmp-lrdmoy
  8. Zip_safe flag not set; analyzing archive contents ...
  9. Adding Pymongo 3.4. 0 to easy-install.pth file
  10. Installed/usr/lib/python2. 6/site-packages/pymongo-3.4. 0-py2. 6-linux-x86_64.egg
  11. Processing dependencies for Pymongo
  12. Finished processing dependencies for Pymongo
  13. [[email protected]03_SDWM ~]#


3, use the Pymongo operation MongoDB to do some simple operation to the MongoDB library

[Python]View PlainCopy
    1. #!/usr/bin/env python
    2. #-*-Coding:utf-8-*-
    3. Import Pymongo
    4. Import datetime
    5. Def get_db ():
    6. # Establish a connection
    7. Client = Pymongo. Mongoclient (host="10.244.25.180", port=27017)
    8. db = client[' example ')
    9. #或者 db = Client.example
    10. return DB
    11. def get_collection (db):
    12. # Select Collection (Mongo in collection and database are created by inertia)
    13. coll = db[' informations ']
    14. print db.collection_names ()
    15. return Coll
    16. def insert_one_doc (db):
    17. # Insert a document
    18. coll = db[' informations ']
    19. Information = {"name": "Quyang", "age": " +"}
    20. information_id = Coll.insert (information)
    21. Print information_id
    22. def insert_multi_docs (db):
    23. # Insert documents in bulk, insert an array
    24. coll = db[' informations ']
    25. Information = [{"name": " xiaoming", "age": "+" }, {"name": "Xiaoqiang", "age": "24"}]
    26. information_id = Coll.insert (information)
    27. Print information_id
    28. def get_one_doc (db):
    29. # There's just one return, no return none
    30. coll = db[' informations ']
    31. print Coll.find_one () # returns the first record
    32. Print Coll.find_one ({"name": "Quyang"})
    33. Print Coll.find_one ({"name": "None"})
    34. def get_one_by_id (db):
    35. # Find a doc with Objectid
    36. coll = db[' informations ']
    37. obj = Coll.find_one ()
    38. obj_id = obj["_id"]
    39. print "_id for Objectid type, obj_id:" + str (obj_id)
    40. Print Coll.find_one ({"_id": obj_id})
    41. # Note that the obj_id here is an object, not a str, and cannot be found using the STR type as the _id value.
    42. print "_id for str type"
    43. Print Coll.find_one ({"_id": Str (OBJ_ID)})
    44. # The Objectid method can be used to turn str into Objectid type
    45. From bson.objectid import objectid
    46. Print "_id converted to Objectid type"
    47. Print Coll.find_one ({"_id": ObjectId (str (obj_id))})
    48. def get_many_docs (db):
    49. # 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 .
    50. coll = db[' informations ']
    51. #ASCENDING = 1 ascending; descending = 1 descending; default is ascending
    52. for item in Coll.find (). Sort ("age", Pymongo. Descending):
    53. Print Item
    54. Count = Coll.count ()
    55. print "All data in collection%s"% int (count)
    56. #条件查询
    57. Count = Coll.find ({"name": "Quyang"}). Count ()
    58. print "Quyang:%s"%count
    59. def clear_all_datas (db):
    60. #清空一个集合中的所有数据
    61. db["informations"].remove ()
    62. if __name__ = = ' __main__ ':
    63. db = get_db ()
    64. My_collection = get_collection (db)
    65. Post = {"author": "Mike", "text": "My First blog post!", "tags": ["MongoDB", "Python", "py" Mongo "],
    66. "Date": Datetime.datetime.utcnow ()}
    67. # Insert Record
    68. My_collection.insert (POST)
    69. Insert_one_doc (DB)
    70. # Conditional Query
    71. Print My_collection.find_one ({"x": "Ten"})
    72. # Query All the data in the table
    73. For III in my_collection.find ():
    74. Print III
    75. print My_collection.count ()
    76. My_collection.update ({"author": "Mike"},
    77. {"author": " Quyang", "text": "My First blog post!", "tags": ["MongoDB", "Python ", " Pymongo "],
    78. "Date": Datetime.datetime.utcnow ()})
    79. For jjj in my_collection.find ():
    80. Print JJJ
    81. Get_one_doc (DB)
    82. GET_ONE_BY_ID (DB)
    83. Get_many_docs (DB)
    84. # Clear_all_datas (db)

Python uses Pymongo to manipulate the MongoDB library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.