Python connection MongoDB operation Data sample (MONGODB Database configuration Class) _python

Source: Internet
Author: User
Tags mongodb

First, related code
Database Configuration Class mongodbconn.py

Copy Code code as follows:

#encoding =utf-8
'''

Mongo Conn Connection Class
'''

Import Pymongo

Class Dbconn:
conn = None
Servers = "mongodb://localhost:27017"

def connect (self):
Self.conn = Pymongo. Connection (Self.servers)

def close (self):
Return Self.conn.disconnect ()

def getconn (self):
Return Self.conn

Mongodemo.py class

Copy Code code as follows:

#encoding =utf-8
'''

MONGO Operation Demo
Done:
'''
Import Mongodbconn

Dbconn = Mongodbconn.dbconn ()
conn = None
Lifeba_users = None

def process ():
#建立连接
Dbconn.connect ()
Global Conn
conn = Dbconn.getconn ()

#列出server_info信息
Print Conn.server_info ()

#列出全部数据库
databases = Conn.database_names ()
Print databases

#删除库和表
Droptable ()
#添加数据库lifeba及表 (Collections) Users
CreateTable ()
#插入数据
Insertdatas ()
#更新数据
UpdateData ()
#查询数据
Querydata ()
#删除数据
DeleteData ()

#释放连接
Dbconn.close ()

Def insertdatas ():
datas=[{"name": "Steven1", "realname": "Test 1", "Age": 25},
{"Name": "Steven2", "realname": "Test 2", "Age": 26},
{' name ': ' Steven1 ', ' realname ': ' Test 3 ', ' Age ': 23}]
Lifeba_users.insert (Datas)

Def updatedata ():
"Modify only the last matching data
The 3rd argument is set to true, and the data is not found to add a
The 4th argument is set to True, and multiple records are not updated
'''
Lifeba_users.update ({' name ': ' Steven1 '},{' $set ': {' realname ': ' Test 1 Modify '}}, False,false)

Def deletedata ():
Lifeba_users.remove ({' name ': ' Steven1 '})

Def querydata ():
#查询全部数据
rows = Lifeba_users.find ()
Printresult (rows)
#查询一个数据
Print Lifeba_users.find_one ()
#带条件查询
Printresult (Lifeba_users.find ({' name ': ' Steven2 '}))
Printresult (Lifeba_users.find ({' name ': {' $gt ': 25}}))

Def createtable ():
' Create libraries and tables '
Global Lifeba_users
Lifeba_users = Conn.lifeba.users

Def droptable ():
"Delete Table" "
Global Conn
Conn.drop_database ("Lifeba")

def printresult (rows):
For row in rows:
For key in Row.keys (): #遍历字典
Print Row[key], #加, do not wrap lines
print '

if __name__ = = ' __main__ ':
Process ()

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.