About Me

My photo
HI , this is Shivakant working in IT Infrastructure from about 20 years now .Implemented many small and large data centers , Executed huge Storage Migration , Implemented Backup Infrastructure for many small and Big organizations.I am a passionate learner of new technology , Love to fiddle with gadgets and read about them on internet. love to read books .

Wednesday, July 25, 2018

CONNECTING AND TESTING MONGODB USING PYTHON

ALL DOCUMENT ARE PROPERTY OF SHIVAKANT , DO NOT COPY IBM , AIX , HACMP , ARE REGISTERED TRADE MARK OF IBM, NETAPP IS REGISTERED TRADEMARK OF NETAPP
 http://aix-administration.blogspot.com/p/index-of-posts-available-in-this-blog.html


CONNECTING AND TESTING MONGODB USING PYTHON

[root@localhost ~]# python
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> from pymongo import MongoClient
>>> client = MongoClient()
>>> db = client.mydb
>>> collection = db.staff


VIEWING DATABASE DETAILS FROM MONGODB IN PYTHON
>>> ret = collection.find()
>>> for record in ret:
...     print(record)
...
{u'salary': 40000, u'name': u'akash', u'age': 20, u'experience': 10, u'sex': u'male', u'location': u'delhi', u'skill': u'storage', u'_id': ObjectId('5b4b5db7e90739b5b94ceff7')}
{u'salary': 50000, u'name': u'ananya', u'age': 23, u'experience': 12, u'sex': u'female', u'location': u'delhi', u'skill': u'os', u'_id': ObjectId('5b4b5db7e90739b5b94ceff8')}
{u'salary': 60000, u'name': u'shiva', u'age': 24, u'experience': 10, u'sex': u'male', u'location': u'noida', u'skill': u'backup', u'_id': ObjectId('5b4b5db7e90739b5b94ceff9')}
{u'salary': 60000, u'name': u'rohan', u'age': 30, u'experience': 10, u'sex': u'male', u'location': u'delhi', u'skill': u'storage', u'_id': ObjectId('5b4b5db7e90739b5b94ceffa')}
{u'salary': 12000, u'name': u'reema', u'age': 35, u'experience': 5, u'sex': u'female', u'location': u'bangalore', u'skill': u'os', u'_id': ObjectId('5b4b5db7e90739b5b94ceffb')}
{u'salary': 12000, u'name': u'ramesh', u'age': 37, u'experience': 4, u'sex': u'male', u'location': u'bangalore', u'skill': u'backup', u'_id': ObjectId('5b4b5db7e90739b5b94ceffc')}
{u'salary': 60000, u'name': u'dinesh', u'age': 40, u'experience': 10, u'sex': u'male', u'location': u'hydrabad', u'skill': u'linux', u'_id': ObjectId('5b4b5db7e90739b5b94ceffd')}
{u'salary': 75000, u'name': u'juhi', u'age': 28, u'experience': 15, u'sex': u'female', u'location': u'hydrabad', u'skill': u'linux', u'_id': ObjectId('5b4b5db7e90739b5b94ceffe')}
{u'salary': 120000, u'name': u'mahesh', u'age': 39, u'experience': 20, u'sex': u'male', u'location': u'bangalore', u'skill': u'aix', u'_id': ObjectId('5b4b5db7e90739b5b94cefff')}
{u'salary': 120000, u'name': u'mayank', u'age': 31, u'experience': 20, u'sex': u'male', u'location': u'delhi', u'skill': u'aix', u'_id': ObjectId('5b4b5db7e90739b5b94cf000')}
{u'salary': 90000, u'name': u'shshank', u'age': 28, u'experience': 16, u'sex': u'male', u'location': u'noida', u'skill': u'sap', u'_id': ObjectId('5b4b5db7e90739b5b94cf001')}
{u'salary': 80000, u'name': u'rohit', u'age': 38, u'experience': 12, u'sex': u'male', u'location': u'delhi', u'skill': u'sap', u'_id': ObjectId('5b4b5db7e90739b5b94cf002')}
>>> 
 

INSTALL PY MONGO ON CENTOS 7

ALL DOCUMENT ARE PROPERTY OF SHIVAKANT , DO NOT COPY IBM , AIX , HACMP , ARE REGISTERED TRADE MARK OF IBM, NETAPP IS REGISTERED TRADEMARK OF NETAPP  
http://aix-administration.blogspot.com/p/index-of-posts-available-in-this-blog.html



INSTALL EPEL RELEASE

[root@localhost ~]# yum install epel-release
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * elrepo: mirrors.thzhost.com
 * extras: centos.excellmedia.net
 * updates: centos-hn.viettelidc.com.vn
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch             Version         Repository        Size
================================================================================
Installing:
 epel-release           noarch           7-11            extras            15 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]: y
Downloading packages:
epel-release-7-11.noarch.rpm   FAILED
http://centos.mirror.snu.edu.in/centos/7.5.1804/extras/x86_64/Packages/epel-release-7-11.noarch.rpm: [Errno 14] curl#6 - "Could not resolve host: centos.mirror.snu.edu.in; Unknown error"
Trying other mirror.
epel-release-7-11.noarch.rpm                               |  15 kB   00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                     1/1
  Verifying  : epel-release-7-11.noarch                                     1/1

Installed:
  epel-release.noarch 0:7-11

Complete!
[root@localhost ~]#

INSTALL PYMONGO AND PIP

[root@localhost ~]# yum -y install python-pymongo
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.nhanhoa.com
 * elrepo: ftp.e-utp.net
 * epel: ftp.jaist.ac.jp
 * extras: mirrors.123host.vn
 * updates: mirrors.viethosting.com
Resolving Dependencies
--> Running transaction check
---> Package python-pymongo.x86_64 0:2.5.2-4.el7 will be installed
--> Processing Dependency: python-bson = 2.5.2-4.el7 for package: python-pymongo-2.5.2-4.el7.x86_64
--> Running transaction check
---> Package python-bson.x86_64 0:2.5.2-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch           Version               Repository    Size
================================================================================
Installing:
 python-pymongo         x86_64         2.5.2-4.el7           epel         199 k
Installing for dependencies:
 python-bson            x86_64         2.5.2-4.el7           epel         111 k

Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 310 k
Installed size: 1.4 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/python-pymongo-2.5.2-4.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for python-pymongo-2.5.2-4.el7.x86_64.rpm is not installed
(1/2): python-pymongo-2.5.2-4.el7.x86_64.rpm               | 199 kB   00:01
(2/2): python-bson-2.5.2-4.el7.x86_64.rpm                  | 111 kB   00:01
--------------------------------------------------------------------------------
Total                                              157 kB/s | 310 kB  00:01
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python-bson-2.5.2-4.el7.x86_64                               1/2
  Installing : python-pymongo-2.5.2-4.el7.x86_64                            2/2
  Verifying  : python-pymongo-2.5.2-4.el7.x86_64                            1/2
  Verifying  : python-bson-2.5.2-4.el7.x86_64                               2/2

Installed:
  python-pymongo.x86_64 0:2.5.2-4.el7

Dependency Installed:
  python-bson.x86_64 0:2.5.2-4.el7

Complete!
[root@localhost ~]# yum -y install python*pip
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.mirror.snu.edu.in
 * elrepo: elrepo.mirrors.arminco.com
 * epel: epel.scopesky.iq
 * extras: centos-hn.viettelidc.com.vn
 * updates: centos-hn.viettelidc.com.vn
Resolving Dependencies
--> Running transaction check
---> Package python2-pip.noarch 0:8.1.2-6.el7 will be installed
---> Package python34-pip.noarch 0:8.1.2-6.el7 will be installed
--> Processing Dependency: python(abi) = 3.4 for package: python34-pip-8.1.2-6.el7.noarch
--> Processing Dependency: python34-setuptools for package: python34-pip-8.1.2-6.el7.noarch
--> Processing Dependency: /usr/bin/python3.4 for package: python34-pip-8.1.2-6.el7.noarch
--> Running transaction check
---> Package python34.x86_64 0:3.4.8-1.el7 will be installed
--> Processing Dependency: python34-libs(x86-64) = 3.4.8-1.el7 for package: python34-3.4.8-1.el7.x86_64
--> Processing Dependency: libpython3.4m.so.1.0()(64bit) for package: python34-3.4.8-1.el7.x86_64
---> Package python34-setuptools.noarch 0:19.6.2-3.el7 will be installed
--> Running transaction check
---> Package python34-libs.x86_64 0:3.4.8-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                    Arch          Version             Repository   Size
================================================================================
Installing:
 python2-pip                noarch        8.1.2-6.el7         epel        1.7 M
 python34-pip               noarch        8.1.2-6.el7         epel        1.7 M
Installing for dependencies:
 python34                   x86_64        3.4.8-1.el7         epel         51 k
 python34-libs              x86_64        3.4.8-1.el7         epel        8.3 M
 python34-setuptools        noarch        19.6.2-3.el7        epel        414 k

Transaction Summary
================================================================================
Install  2 Packages (+3 Dependent packages)

Total download size: 12 M
Installed size: 45 M
Downloading packages:
(1/5): python34-3.4.8-1.el7.x86_64.rpm                     |  51 kB   00:02
(2/5): python2-pip-8.1.2-6.el7.noarch.rpm                  | 1.7 MB   00:10
(3/5): python34-setuptools-19.6.2-3.el7.noarch.rpm         | 414 kB   00:11
(4/5): python34-pip-8.1.2-6.el7.noarch.rpm                 | 1.7 MB   00:18
(5/5): python34-libs-3.4.8-1.el7.x86_64.rpm                | 8.3 MB   00:24
--------------------------------------------------------------------------------
Total                                              508 kB/s |  12 MB  00:24
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python34-3.4.8-1.el7.x86_64                                  1/5
  Installing : python34-libs-3.4.8-1.el7.x86_64                             2/5
  Installing : python34-setuptools-19.6.2-3.el7.noarch                      3/5
  Installing : python34-pip-8.1.2-6.el7.noarch                              4/5
  Installing : python2-pip-8.1.2-6.el7.noarch                               5/5
  Verifying  : python34-libs-3.4.8-1.el7.x86_64                             1/5
  Verifying  : python34-3.4.8-1.el7.x86_64                                  2/5
  Verifying  : python2-pip-8.1.2-6.el7.noarch                               3/5
  Verifying  : python34-setuptools-19.6.2-3.el7.noarch                      4/5
  Verifying  : python34-pip-8.1.2-6.el7.noarch                              5/5

Installed:
  python2-pip.noarch 0:8.1.2-6.el7       python34-pip.noarch 0:8.1.2-6.el7

Dependency Installed:
  python34.x86_64 0:3.4.8-1.el7              python34-libs.x86_64 0:3.4.8-1.el7
  python34-setuptools.noarch 0:19.6.2-3.el7

Complete!
[root@localhost ~]#


KeyboardInterrupt

RUN PIP 
 
[root@localhost ~]# pip install pymongo
Requirement already satisfied (use --upgrade to upgrade): pymongo in /usr/lib64/python2.7/site-packages
[root@localhost ~]#
 

flipkart3

Total Pageviews