Python access MongoDB environment installation and testing

Source: Internet
Author: User

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'}

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.