Python操作MongoDB
Python操作MongoDB#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time: 2017/8/3 10:11# @File: Demo1.py"""操作mongodb数据库"""import pymongoclient = pymongo.MongoClient('mongodb://localhos
·
Python操作MongoDB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/8/3 10:11
# @File : Demo1.py
"""
操作mongodb数据库
"""
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017') #配置连接
db_name = 'flask' #数据库名字
db = client[db_name]
collection = db['users'] #集合名字
#插入数据
def insert():
users = {"name": "xiassss", "age": "23"}
collection.insert(users) # 添加数据
collection.save(users) # 添加数据
#批量插入
def insert_many():
users=[{'name':'LIM','age':'55'},{'name':'MMM','age':'77'}]
for user in users:
collection.insert(user)
collection.save(user)
#条件查询
def select():
data=collection.find_one({'name':'LIM'})
print data
#移除数据
def remove():
collection.remove({'name':'MMM'})
#数据更新
def replace():
collection.replace_one({'name':'LIM'},{'name':'RRR'})
更多推荐
已为社区贡献3条内容
所有评论(0)