MongoDB interacts with Python

Source: Internet
Author: User
Tags mongoclient virtual environment

1.Pymongo

Pymongo is the Python interface development package for MongoDB and is the recommended way to use Python and MongoDB.
Official documents

2. Installation
Go to Virtual Environment sudo pip install Pymongo or source installation python setup.py
3. Use

Import Module

Import Pymongo # or  from Import Mongoclient

Connections Built on Mongoclient:

Client = mongoclient ('localhost', 27017#client = mongoclient ( ' mongodb://localhost:27017/ ')

Get the database

db = client.test_database#  or db = client['test-database' ]

Get a collection of data

Collection = db.test_collection#collection = db['test-collection' ]
4. Complete the Command line project: Student Information Management (based on Python2.7)

Code manipulation

#-*-coding:utf-8-*-ImportPymongodefsystem ():Print('you will enter the database management system, the data priceless, careful operation! ◆')    Print('◇ 1: View data ◇')    Print('◇ 2: Increase data ◇')    Print('◇ 3: Modify Data ◇')    Print('◇ 4: Delete data ◇')    Print('◇ 5: Search Data ◇')    Print('6: Exit the database management system')    #establish a connection to MongoDBClient = Pymongo. Mongoclient ('localhost', 27017)    #Get the databaseStu = client['Stu']    #get a collection of datamessage = stu['message']     whileTrue:order= Int (Raw_input ('Please enter the relevant instructions:'))        ifOrder==1: Exit=Message.count ()ifexit==0:Print('Sorry, there is currently no data in the database! ')            Else:                 forDatainchmessage.find (): Content= data['name']+data[' Age']+data['Sex']                    Print(content)elifOrder ==2: Name= Raw_input ('Please enter student's name:') Age= Raw_input ('Please enter student's age:') Sex= Raw_input ('Please enter student gender (male/female):') Data= {                'name': Name,' Age': Age,'Sex': Sex,} message.insert_one (data)Print('Add success! ')        elifOrder = = 3: Name= Raw_input ('Please enter the name of the student to be modified:') Exit= Message.count ({'name': Name}) ifExit! =0:age= Raw_input ('Please enter the age of the modified student:') message.update ({'name': name},{'$set':{' Age': Age}}) Print('Modification succeeded')            Else:                Print 'Sorry, there is no information for this student in the database! '        elifOrder = = 4: Name= Raw_input ('Please enter the name of the student you want to delete:') Exit= Message.count ({'name': Name}) ifExit! =0:message.remove ({'name': Name}) Print('Delete succeeded')            Else:                Print 'Sorry, there is no information for this student in the database! '        elifOrder = = 5: Name= Raw_input ('Please enter the name of the student to be queried:') Exit= Message.count ({'name': Name}) ifexit!=0:data= Message.find_one ({'name': Name}) Content= data['name']+data[' Age']+data['Sex']                PrintcontentElse:                Print 'Sorry, there is no information for this student in the database! '        elifOrder = = 6:            Print('Thank you for your use! ')             Break        Else:            Print('your input is incorrect, please enter a valid instruction (1/2/3/4/5)')if __name__=='__main__': System ()


Test results

MongoDB interacts with Python

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.