2025-11-12 14:10:05 +08:00

83 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ============================================
// 火箭系统MongoDB索引初始化脚本
// 执行方式: mongosh < rocket_mongo_init.js
// ============================================
// 切换到likei数据库 (根据实际数据库名修改)
use likei
print("开始创建火箭系统MongoDB索引...");
// 1. room_rocket_status 集合索引
print("1. 创建 room_rocket_status 索引...");
// 唯一索引房间ID+日期
db.room_rocket_status.createIndex(
{ "room_id": 1, "date": 1 },
{ unique: true, name: "uk_room_date", background: true }
);
print(" ✓ 创建唯一索引 uk_room_date");
// 普通索引:状态
db.room_rocket_status.createIndex(
{ "status": 1 },
{ name: "idx_status", background: true }
);
print(" ✓ 创建索引 idx_status");
// 普通索引:状态+过期时间 (用于定时任务清理)
db.room_rocket_status.createIndex(
{ "status": 1, "claim_expire_time": 1 },
{ name: "idx_status_expire", background: true }
);
print(" ✓ 创建索引 idx_status_expire");
// TTL索引30天自动过期
db.room_rocket_status.createIndex(
{ "updated_at": 1 },
{ expireAfterSeconds: 2592000, name: "ttl_30days", background: true }
);
print(" ✓ 创建TTL索引 ttl_30days (30天过期)");
// 2. rocket_launch_history 集合索引
print("2. 创建 rocket_launch_history 索引...");
// 普通索引房间ID+发射时间 (倒序)
db.rocket_launch_history.createIndex(
{ "room_id": 1, "launch_time": -1 },
{ name: "idx_room_launch_time", background: true }
);
print(" ✓ 创建索引 idx_room_launch_time");
// 普通索引:日期
db.rocket_launch_history.createIndex(
{ "date": 1 },
{ name: "idx_date", background: true }
);
print(" ✓ 创建索引 idx_date");
// 普通索引:等级
db.rocket_launch_history.createIndex(
{ "level": 1 },
{ name: "idx_level", background: true }
);
print(" ✓ 创建索引 idx_level");
// 普通索引:发射时间 (用于时间范围查询)
db.rocket_launch_history.createIndex(
{ "launch_time": -1 },
{ name: "idx_launch_time", background: true }
);
print(" ✓ 创建索引 idx_launch_time");
print("\n索引创建完成\n");
// 验证索引
print("验证 room_rocket_status 索引:");
printjson(db.room_rocket_status.getIndexes());
print("\n验证 rocket_launch_history 索引:");
printjson(db.rocket_launch_history.getIndexes());
print("\n火箭系统MongoDB初始化完成!");