Python操作MongoDB
2023-02-18 16:39:33 时间
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)
相关文章
- 痞子衡嵌入式:i.MXRT中FlexSPI外设不常用的读选通采样时钟源 - loopbackFromSckPad
- 痞子衡嵌入式:RT-MFB - 一种灵活的i.MXRT下多串行NOR Flash型号选择的量产方案
- 《痞子衡嵌入式半月刊》 第 64 期
- 痞子衡嵌入式:浅谈i.MXRT10xx系列MCU外接24MHz晶振的作用
- 痞子衡嵌入式:一个关于Segger J-Flash在Micron Flash固定区域下载校验失败的故事(SR寄存器BP[x:0]位)
- 痞子衡嵌入式:MCUXpresso IDE下生成镜像文件的方法及其与IAR,MDK差异
- 《痞子衡嵌入式半月刊》 第 63 期
- 《痞子衡嵌入式半月刊》 第 62 期
- 痞子衡嵌入式:理解i.MXRT中FlexSPI外设lookupTable里配置访问行列混合寻址Memory的参数值
- 《痞子衡嵌入式半月刊》 第 61 期
- 痞子衡嵌入式:在i.MXRT启动头FDCB里使能串行NOR Flash的QPI/OPI模式
- 痞子衡嵌入式:MCUXpresso IDE下设置代码编译优化等级的几种方法
- 痞子衡嵌入式:浅析IAR下调试信息输出机制之半主机(Semihosting)
- 痞子衡嵌入式:浅析IAR下调试信息输出机制之硬件UART外设
- 痞子衡嵌入式:聊聊i.MXRT1170双核下不同GPIO组的访问以及中断设计
- 《痞子衡嵌入式半月刊》 第 60 期
- 《痞子衡嵌入式半月刊》 第 59 期
- 痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
- 《痞子衡嵌入式半月刊》 第 58 期
- 《痞子衡嵌入式半月刊》 第 57 期