
{"id":7221,"date":"2021-04-13T14:45:17","date_gmt":"2021-04-13T14:45:17","guid":{"rendered":"https:\/\/rax-stg-wp-aws.com\/blogs\/pymongo-and-other-python-modules-for-database-engines\/"},"modified":"2026-11-03T19:28:15","modified_gmt":"2026-11-03T19:28:15","slug":"pymongo-and-other-python-modules-for-database-engines","status":"publish","type":"blogs","link":"https:\/\/www.rackspace.com\/en-gb\/blogs\/pymongo-and-other-python-modules-for-database-engines\/","title":{"rendered":"PyMongo and other Python modules for database engines"},"content":{"rendered":"<p><strong>Overview<\/strong><\/p>\n<p>In general, developers use shell scripts to automate database tasks such as exporting and importing data, gathering database (DB) statistics, tablespace monitoring, blocking sessions, DB locks, and so on.&nbsp;<\/p>\n<p>Python has become one of the most popular programming languages over the last seven or more years. This post covers different DB modules used in Python for various database engines and shows how to use the PyMongo module to query and insert data into a MongoDB\u00ae collection.<\/p>\n<p><strong>Intro to Python modules<\/strong><\/p>\n<p>Python is an object-oriented and high-level programming language. It has a wide range of modules for different implementations such as web development, data science, Linux\u00ae administration, and so on. The following table shows details about the database engine and relevant Python modules:<\/p>\n<p>| Database Engine | Python Module |<br \/>\n| &#8212; | &#8212; |&nbsp;&nbsp; &nbsp;&nbsp;<br \/>\n| Oracle | cx_oracle |<br \/>\n| PostgreSQL | psycopg2 |<br \/>\n| MongoDB |pymongo |<br \/>\n| MySQL | mysql.connector and pymysql |<br \/>\n| Cassandra | cassandra-driver |<\/p>\n<p><strong>CX_ORACLE<\/strong><\/p>\n<p><em>cx_oracle<\/em>&nbsp;is a Python module that allows you to access Oracle databases and adheres to Python database API specifications. This module is compatible with Oracle Database 11.2, 12c, 18c,19c, and 21c.<\/p>\n<p><strong>PSYCOPG2<\/strong><\/p>\n<p><em>psycopg2<\/em> is a Python module that allows you to access PostgreSQL databases and adheres to Python database API specifications. One of the key features is that it enables multi-threaded applications that open multiple cursors to perform concurrent inserts and updates.<\/p>\n<p><strong>PYMONGO&nbsp;<\/strong><\/p>\n<p><em>pymongo<\/em>&nbsp;is a module that allows you to access mongo databases and perform database queries on MongoDB database objects such as collections, documents, and so on.<\/p>\n<p><strong>MYSQL.CONNECTOR<\/strong><\/p>\n<p><em>mysql.connector<\/em>&nbsp;is a module that allows you to access MySQL databases and adheres to Python database API specifications. It enables database queries.<\/p>\n<p><strong>CASSANDRA-DRIVER<\/strong><\/p>\n<p><em>cassandra-driver<\/em> is a Python driver for Apache Cassandra databases and is compatible with the Cassandra query language (CQL). By using this module, you can connect to and query Cassandra databases.<\/p>\n<p><strong>&nbsp;PyMongo demonstration<\/strong><\/p>\n<p>Python programming performs different CRUD (Create, Read, Update, and Delete) operations in MongoDB. The following steps show you how to download pip, install the <em>pymongo<\/em>&nbsp;module, and write and execute a Python script with PyMongo:<\/p>\n<p>\n1. Run the following command to check the version of Python on your Linux server:<\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; # python &#8211;version<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; Python 2.7.18<\/em><\/p>\n<p>2. Use the following commands to install pip and &nbsp;PyMongo:<\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; # yum install python-pip<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; # pip install pymongo<\/em><\/p>\n<p>3. Open the text editor of your choice and write the following Python script<br \/>\n&nbsp; &nbsp;(<strong>mongo-crud.py<\/strong>) to perform CRUD operations on a collection in the MongoDB:<\/p>\n<p>&nbsp; &nbsp; &nbsp;<em> &nbsp; #!\/usr\/bin\/python<br \/>\n&nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; from pymongo import MongoClient<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; client = MongoClient(host = &#8216;localhost:27017&#8217;, username=&#8221;dba&#8221;, password=&#8221;user123&#8243;)<\/em><br \/>\n&nbsp;<br \/>\n&nbsp;<em> &nbsp; &nbsp; &nbsp; mongodbinfo = client.server_info()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print (&#8220;Printing MongoDB Server Information&#8221;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print (mongodbinfo)<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; print (&#8220;##################################################&#8221;)<\/em><\/p>\n<p>&nbsp; &nbsp; &nbsp;print (&#8220;Mongo DB version&#8221;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print (&#8220;db version:&#8221;, mongodbinfo[&#8220;version&#8221;])<\/p>\n<p>&nbsp; &nbsp; print (&#8220;##################################################&#8221;)<\/p>\n<p><em>db = client.empdb<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; empdetails = db.employeedetails<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print (&#8220;Existing collection information&#8221;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; printcollection = empdetails.find()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; for i in printcollection:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print (i)<\/em><\/p>\n<p><em>&nbsp; &nbsp; print (&#8220;##################################################&#8221;)<\/em><\/p>\n<p><em>print (&#8220;Adding one document to the collection&#8221;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; details = {&#8220;name&#8221;: &#8220;Andy&#8221;, &#8220;designation&#8221;: &#8220;Clerk&#8221;}<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; newentry = empdetails.insert_one(details)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print (&#8221; newentry:&#8221;, newentry)<br \/>\n&nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; printcollection1 = empdetails.find()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; for i in printcollection1:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print (i)<\/em><\/p>\n<p><em>print (&#8220;##################################################&#8221;)<\/em><\/p>\n<p><em>print (&#8220;Performing a search on the collection&#8221;)<\/em><\/p>\n<p><em>&nbsp; search = empdetails.find_one({ &#8216;name&#8217; : &#8216;Jack&#8217;})<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print (search)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print (&#8220;##################################################&#8221;)<\/em><\/p>\n<p><em>print (&#8220;Adding multiple documents to the collection&#8221;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; entry1 = {&#8220;name&#8221;: &#8220;Cathy&#8221;, &#8220;designation&#8221;: &#8220;Data Operator&#8221;}<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; entry2 = {&#8220;name&#8221;: &#8220;Susan&#8221;, &#8220;designation&#8221;: &#8220;Operations Manager&#8221;}<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; newentries = empdetails.insert_many([entry1, entry2])<br \/>\n&nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; printcollection2 = empdetails.find()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; for i in printcollection2:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print (i)<\/em><\/p>\n<p><em>4. Execute the Python script by running the following command. The output looks similar to&nbsp;the following image:&nbsp;<\/em><\/p>\n<p><em>&nbsp; &nbsp;# python mongo-crud.py<\/em><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/26321-body-Picture1_42.png\" alt=\"py mongo pic 1\" \/><\/p>\n<p><strong>Conclusion&nbsp;<\/strong><\/p>\n<p>Python was one of the most trending programming languages in 2020 with a wide range of implementations. Python provides database engineers alternatives to shell scripting for database task automation<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PyMongo and other Python modules for database engines<\/p>\n","protected":false},"author":2,"featured_media":7223,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","format":"standard","meta":{"content-type":"","nid":"26321","content_type":"blog","created_by":"10130","authored_on":"2023-11-30T15:45:17+00:00","field_article_industries":"","tag_category":"Technical","field_blog_author_text":"by Dinesh Miriam, Oracle Database Administrator, Rackspace Technology","seo_description":"Automate DB tasks using Python and PyMongo module for MongoDB collections. Ideal for developers seeking Python-based DB automation.\r\n","field_featured_image":"","insights_image":"\/wp-content\/uploads\/2026\/blog_technical_images\/26321-insights_image-database-technical-blog-1-insights.jpg","field_partners":"","rxt_platforms":"MongoDB","product":"","publisheddate":"2021-04-13T14:45:17","rxt_solutions":"Data, Databases","opengraph_tag":"","x_tag":"","teaser_flag":"Technical Blog","seo_titile":"PyMongo and Python for DB Engines | Rackspace Technology","field_topics":"","field_slug":"pymongo-and-other-python-modules-for-database-engines","footnotes":""},"categories":[178],"tags":[204],"class_list":["post-7221","blogs","type-blogs","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-technical"],"_links":{"self":[{"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/blogs\/7221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/blogs"}],"about":[{"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/types\/blogs"}],"author":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/comments?post=7221"}],"version-history":[{"count":2,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/blogs\/7221\/revisions"}],"predecessor-version":[{"id":9188,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/blogs\/7221\/revisions\/9188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/media\/7223"}],"wp:attachment":[{"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/media?parent=7221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/categories?post=7221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/tags?post=7221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}