92 lines
3.3 KiB
JavaScript
92 lines
3.3 KiB
JavaScript
// 创建 yh_web 所需 MongoDB 集合及索引(mongosh 执行)
|
||
// 用法: mongosh "mongodb://localhost:27017" --file MongoDB/create_collections.js
|
||
// 或: mongosh "mongodb://localhost:27017" MongoDB/create_collections.js
|
||
|
||
const dbName = "yxd-agent-testing";
|
||
const db = db.getSiblingDB(dbName);
|
||
|
||
print("使用数据库: " + dbName);
|
||
|
||
// 1. sites
|
||
if (!db.getCollectionNames().includes("sites")) {
|
||
db.createCollection("sites");
|
||
print("已创建集合: sites");
|
||
}
|
||
db.sites.createIndex({ created_at: -1 }, { name: "idx_created_at", background: true });
|
||
|
||
// 2. pages
|
||
if (!db.getCollectionNames().includes("pages")) {
|
||
db.createCollection("pages");
|
||
print("已创建集合: pages");
|
||
}
|
||
db.pages.createIndex(
|
||
{ site_id: 1, slug: 1 },
|
||
{ unique: true, name: "idx_site_slug", background: true }
|
||
);
|
||
|
||
// 3. site_assets
|
||
if (!db.getCollectionNames().includes("site_assets")) {
|
||
db.createCollection("site_assets");
|
||
print("已创建集合: site_assets");
|
||
}
|
||
db.site_assets.createIndex({ site_id: 1 }, { name: "idx_site_id", background: true });
|
||
|
||
// 4. users
|
||
if (!db.getCollectionNames().includes("users")) {
|
||
db.createCollection("users");
|
||
print("已创建集合: users");
|
||
}
|
||
db.users.createIndex({ username: 1 }, { unique: true, name: "idx_username", background: true });
|
||
db.users.createIndex({ mobile: 1 }, { name: "idx_mobile", background: true, sparse: true });
|
||
|
||
// 5. workspaces
|
||
if (!db.getCollectionNames().includes("workspaces")) {
|
||
db.createCollection("workspaces");
|
||
print("已创建集合: workspaces");
|
||
}
|
||
db.workspaces.createIndex({ user_id: 1 }, { name: "idx_user_id", background: true });
|
||
|
||
// 6. conversations
|
||
if (!db.getCollectionNames().includes("conversations")) {
|
||
db.createCollection("conversations");
|
||
print("已创建集合: conversations");
|
||
}
|
||
db.conversations.createIndex({ user_id: 1 }, { name: "idx_user_id", background: true });
|
||
db.conversations.createIndex({ workspace_id: 1 }, { name: "idx_workspace_id", background: true });
|
||
|
||
// 7. messages(统计用,功能未实现也可先建)
|
||
if (!db.getCollectionNames().includes("messages")) {
|
||
db.createCollection("messages");
|
||
print("已创建集合: messages");
|
||
}
|
||
db.messages.createIndex({ conversation_id: 1 }, { name: "idx_conversation_id", background: true });
|
||
|
||
// 8. files(统计用,功能未实现也可先建)
|
||
if (!db.getCollectionNames().includes("files")) {
|
||
db.createCollection("files");
|
||
print("已创建集合: files");
|
||
}
|
||
db.files.createIndex({ user_id: 1 }, { name: "idx_user_id", background: true });
|
||
|
||
// 9. system_config(支付、短信等配置,_id 为字符串键)
|
||
if (!db.getCollectionNames().includes("system_config")) {
|
||
db.createCollection("system_config");
|
||
print("已创建集合: system_config");
|
||
}
|
||
|
||
// 10. role_permissions(角色权限:role_id -> permissions 数组)
|
||
if (!db.getCollectionNames().includes("role_permissions")) {
|
||
db.createCollection("role_permissions");
|
||
print("已创建集合: role_permissions");
|
||
}
|
||
db.role_permissions.createIndex({ role_id: 1 }, { unique: true, name: "idx_role_id", background: true });
|
||
|
||
// 11. site_users(前台直播弹幕账号,与后台 users 分离)
|
||
if (!db.getCollectionNames().includes("site_users")) {
|
||
db.createCollection("site_users");
|
||
print("已创建集合: site_users");
|
||
}
|
||
db.site_users.createIndex({ username: 1 }, { unique: true, name: "idx_username", background: true });
|
||
|
||
print("集合与索引处理完成。");
|