Ubuntu: After installing MongoDB and Python, how does Phython access the MongoDB database?
Python is a simple programming language and the developed code is very readable.
This article uses the Python driver called PyMongo driver and MongoDB to develop simple, simple, and powerful code.
Install PyMongo Driver
Before writing python code to access the MongoDB database, we need to install the PyMongo driver.
Suppose you have installed Python or later. Install Python from http://python.org/downloadto download the installation media of the tarfile
$ ./configure$ make$ make test$ sudo make install
Follow these steps to install the PyMongo driver:
Step 1:
Download the Python Installation tool. For example
Http://pypi.python.org/pypi/setuptools#using-setuptools-and-easyinstalldownload
Setuptools-0.6c11-py2.7.egg
Step 2:
Media Used for download
$ sudo sh setuptools-0.6c11-py2.7.egg
Step 3:
Install the PyMongo module:
$ sudo easy_install pymongo
Okay! In Python shell, enter:
import pymongo
Return to the cursor>, and the operation is successful.
Further, insert the data to the database and retrieve it. Remember that in MongoDB, if the retrieved database does not exist, MongoDB will automatically create a database. The following uses mytestdeb as an example to test it.
Input the following in Python shell:
from pymongo import Connectionc = Connection();db = C.mytestdbcollection = db.itemsitem = {“Title”: “Test Data”, “Value1”:”1”, “Value2”:”2”}collection.insert(item)collection.find_one()
The result of executing the last row is:
{u'_id': ObjectId('4d432adc1d41c85d8a000000'), u'Value1': u'1', u'Value2': u'2', u'Title': u'Test Data'}