python 操作MongoDB非关系型数据库

# 导入MongDB模块
import pymongo
from pymongo import MongoClient

# 连接本地服务器

conn = MongoClient("localhost", 27017)

# 连接数据库
db = conn.zhang

# 获取集合student 表名
collection = db.student
"""
# 统计数据数
res = collection.find().count()

# 查询全部数据
res = collection.find()

# 升序
res = collection.find().sort('age')

# 降序
res = collection.find().sort('age', pymongo.DESCENDING)

# 更新数据
collection.updateOne({'name':'yuyue'},{'$set':{'age':3333}})

# 删除条件数据
collection.remove({'name':'yuyue'})

# 全部删除数据
collection.remove()

# 插入数据
collection.insert({'name':'zhangguanghe', 'age':22, 'addr': '丽水金阳', 'phone': 18698828830})

# 条件查询
res = collection.find({'age':{'$ne':22}})
"""

# 断开连接
conn.close()

猜你喜欢

转载自www.cnblogs.com/zhangguanghe/p/9260996.html