| 12345678910111213141516171819202122232425262728293031323334353637 |
- # coding: utf-8
- from pymongo import MongoClient
- import bcrypt
-
- # MongoDB的连接信息
- MONGO_URI = 'mongodb://127.0.0.1:27017'
- MONGO_DBNAME = 'starter_db'
-
- # 创建MongoDB的连接
- client = MongoClient(MONGO_URI)
-
- # 选择或创建数据库
- db = client[MONGO_DBNAME]
-
- # 选择或创建集合(类似关系数据库中的表)
- users_collection = db['users']
-
- # 清空集合,如果集合已经存在并有数据
- users_collection.delete_many({})
-
-
- # 插入一些示例数据
- sample_users = [
- {"username": "developuser", "email": "developuser@example.com", "avatar_url": "https://afanai.top:8088/imgs/default_avatar_1.jpeg", "bio": "This account is for develop", "password_hash": bcrypt.hashpw("super123".encode('utf-8'), bcrypt.gensalt())}
- ]
-
- for i in sample_users:
- print(i)
-
- # 执行插入操作
- result = users_collection.insert_many(sample_users)
-
- # 打印插入的文档ID
- print("Inserted document IDs:", result.inserted_ids)
-
- # 关闭数据库连接
- client.close()
|