Python MONGO operation

Source: Internet
Author: User

#-*-Coding:utf-8-*-' Python MONGO operation Demodone: ' from Pymongo import mongoclientconn = Nonetopics = Noneusers=nonedef p Rocess (): connection=mongoclient (' localhost ', 27017) Global Conn conn = connection; #列出server_info信息 #print conn.server_info () #列出全部数据库 databases = Conn.database_names () print databases topics = Conn.node. Topics Users=conn.node.users Ret=users.find ({' LoginName ': ' Dingxiaoyuehao '}) if Ret and Ret.count () > 0:print "Yes" El Se:print "No" #删除库和表 #dropTable () #添加数据库lifeba及表 (collections) Users #createTable () #插入数据 #insertDatas () #更新数据 # UpdateData () #查询数据 #queryData () #删除数据 #deleteData () #释放连接 #dbconn. Close () def insertdatas (): datas=[{"name": "Steven1", " Realname ":" Test 1 "," age ": [+], {" Name ":" Steven2 "," realname ":" Test 2 "," Age ": +}, {" Name ":" Steven1 "," realname ":" Test 3 "," Age ": Topics.insert" (datas) def updatedata (): "Only modify the last match to the data 3rd parameter is set to True, the data is not found to add a 4th parameter set to True, there are multiple records do not update" Topics.update ({' name ': ' Steven1 '},{' $set ': {' realname ': ' Test 1 Modified '}}, False,false) def deleteData (): ToPics.remove ({' name ': ' Steven1 '}) def querydata (): #查询全部数据 rows = Topics.find () printresult (rows) #查询一个数据 print Topics.find_one () #带条件查询 printresult ({' name ': ' Steven2 '}) Printresult (Topics.find ({' name ': {' $gt ': 25}} ) def createtable (): ' Create library and table ' ' Global topics topics = Conn.lifeba.topicsdef droptable (): ' Delete table ' ' Global conn Conn.drop _database ("Lifeba") def printresult (rows): For row in Rows:for key in Row.keys (): #遍历字典 print Row[key].encode (' gb2312 '), #加 , do not wrap print "if __name__ = = ' __main__ ': Process ()

  

Python MONGO operation

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.