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.update_one({'name':'yuyue'},{'$set':{'age':3333}})

# 更新多条数据
collection.update_many({'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}})

#查询包含列表内容的条件
res = collection.find({'age':{'$in':[22]}})

#查询不包含列表内容的条件
res = collection.find({'age':{'$nin':[22]}})
"""
# 断开连接
conn.close()
原文地址:https://www.cnblogs.com/zhangguanghe/p/9260996.html