| mongod --auth --dbpath="/usr/local/mongodb/data" --logpath="/usr/local/mongodb/logs/mongod.log" --install |
| |
| 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) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)