Python操作MongoDB
2023-06-13 09:16:10 时间
mongod --auth --dbpath="/usr/local/mongodb/data" --logpath="/usr/local/mongodb/logs/mongod.log" --install
#创建admin数据表
use admin
#创建管理用户的用户
db.createUser(
{
user: "root",
pwd: "123456",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
#认证
db.auth('user','pass')
#切换数据表
use testdb
#创建当前数据表的操作用户
db.createUser(
{
user: "test",
pwd: "123456",
roles: [ { role: "readWrite", db: "testdb" } ]
}
)
class MONGODB_CONFIG:
HOST = "127.0.0.1"
PORT = 27017
DB_NAME = "testdb"
USER_NAME = "test"
PASSWORD = "123456"
import config
import pymongo
# 创建连接对象 ''' mongodb://localhost:27017/ '''
client = pymongo.MongoClient(host=config.MONGODB_CONFIG.HOST, port=config.MONGODB_CONFIG.PORT)
# 连接数据库
db = client[config.MONGODB_CONFIG.DB_NAME]
db.authenticate(config.MONGODB_CONFIG.USER_NAME, config.MONGODB_CONFIG.PASSWORD,mechanism='SCRAM-SHA-1')
# 连接表
collection = db.news
def init():
#创建连接对象 ''' mongodb://localhost:27017/ '''
client = pymongo.MongoClient(host='127.0.0.1',port=27017)
#指定数据集(数据库名称) ''' client['test'] '''
db = client.testDB
#指定集合 ''' db['students'] '''
return db.students
#插入单条数据
def insert(collection,student):
result = collection.insert(student)
print(result)
#插入多条数据
def insert(collection,students):
result = collection.insert(students)
print(result)
#使用3.x推荐方法插入单条数据
def insert_one(collection,student):
result = collection.insert_one(student)
print(result)
print(result.inserted_id)
#使用3.x推荐方法插入多条数据
def insert_many(collection,students):
result = collection.insert_many(students)
print(result)
print(result.inserted_ids)
#查询单条数据
def find_one(collection,args):
result = collection.find_one(args)
print(type(result))
print(result)
#查询多条数据
def finds(collection,args):
results = collection.find(args)
print(type(results))
print(results)
for result in results:
print(result)
#查询记录数
def count(collection,args):
count = collection.find(args).count()
print(count)
#排序 ''' ASCENDING 升序 DESCENDING 降序 '''
def sort(collection,args,cending):
results = collection.find().sort(args, cending)
print([result[args] for result in results])
#偏移n位后查询
def skip(collection,index):
results = collection.find().skip(index)
print([result['name'] for result in results])
#获取n条记录
def limit(collection,index):
results = collection.find().limit(index)
print([result['name'] for result in results])
#更新数据库
def update(condition,student):
result = collection.update(condition, student)
print(result)
#删除数据 delete_one() delete_many()
def remove(collection,args):
result = collection.remove(args)
print(result)
相关文章
- Python 编程 | 连载 07 - 列表与元组操作
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(二)
- python-Python与MongoDB数据库-处理MongoDB查询结果
- Python简易操作MySQL入门教程(python操作mysql)
- 使用Python操作MySQL数据库快速上手(python访问mysql数据库)
- MongoDB数据库备份策略实施(mongodb备份命令)
- MongoDB监控软件:有效精確管理你的数据库(mongodb监控软件)
- 库MongoDB:轻松实现只读库(mongodb只读)
- Python驱动Oracle数据库(python操作oracle)
- MongoDB清空表:操作指南(mongodb清空表)
- MongoDB设置过期时间的实践(mongodb过期时间)
- MongoDB分片不均衡问题分析(mongodb分片不均匀)
- MongoDB 数据备份:保险起见,手动操作或自动化都应该掌握!(mongodb备份数据)
- 数据不断,永不脱链!深度剖析兄弟连 MongoDB(兄弟连mongodb)
- MongoDB数据库删除操作:轻松清理空间(mongodb删库)
- 行操作 快速掌握MongoDB命令行操作(mongodb的命令)
- MongoDB数据库中添加字段的操作(mongodb添加字段)
- Mongodb数据一目了然:查看数据操作全面解析(mongodb查看数据)
- MongoDB操作工具类:简单高效易用(mongodb操作工具类)
- 玩转MongoDB:掌握强大的操作工具类(mongodb操作工具类)
- 轻松连接MongoDB探秘命令行操作(mongodb命令行连接)
- MongoDB如何实现高效分页操作?(mongodb 分页)
- MongoDB与C的完美结合(mongodb(c#))
- MongoDB启动命令,快速搭建数据库环境(mongodb启动命令)
- 使用MongoDB进行字段相减操作(mongodb字段相减)