refactor(hospital): 合并复旦榜字段为 fudanRank 对象

This commit is contained in:
lik
2026-11-05 15:46:00 +08:00
parent bb99302041
commit 770f7ebb1d
2 changed files with 47 additions and 27 deletions
+20 -7
View File
@@ -1,8 +1,9 @@
/**
* 一次性回填:按复旦综合排行榜给存量医院写入 fudanRank / fudanGrade / fudanOrder。
* 一次性回填/迁移:把复旦榜信息写成 fudanRank: { rank, grade, order }。
*
* 用法:在 api/health 下执行 `node scripts/backfill_hospital_fudan_rank.js`
* 未上榜医院写默认档位 999 / 默认顺序 9999,保证接口排序时排在所有上榜医院之后。
* 脚本同时清理旧的扁平字段 fudanGrade / fudanOrder。
*/
import { DBModel } from "../models/index.js";
import {
@@ -15,10 +16,12 @@ DBModel.init();
const conn = DBModel.dbConnection;
async function backfill() {
const hospitals = await DBModel.Hospital.find(
// 存量文档可能是旧的扁平字段,直接用原生 collection 读取,避免新旧 schema 互转
const rawCollection = conn.db.collection("hospital");
const hospitals = await rawCollection.find(
{},
{ name: 1, entityName: 1, fudanRank: 1, fudanGrade: 1, fudanOrder: 1 }
).lean();
{ projection: { name: 1, entityName: 1, fudanRank: 1, fudanGrade: 1, fudanOrder: 1 } }
).toArray();
const operations = [];
let ranked = 0;
@@ -30,18 +33,28 @@ async function backfill() {
const order = fudan ? fudan.order : DEFAULT_FUDAN_ORDER;
if (fudan) ranked += 1;
if (hospital.fudanRank === rank && hospital.fudanGrade === grade && hospital.fudanOrder === order) continue;
const current = hospital.fudanRank;
const isNested = current && typeof current === "object";
const unchanged = isNested
&& current.rank === rank
&& current.grade === grade
&& current.order === order;
const hasLegacyFields = hospital.fudanGrade !== undefined || hospital.fudanOrder !== undefined;
if (unchanged && !hasLegacyFields) continue;
operations.push({
updateOne: {
filter: { _id: hospital._id },
update: { $set: { fudanRank: rank, fudanGrade: grade, fudanOrder: order } },
update: {
$set: { fudanRank: { rank, grade, order } },
$unset: { fudanGrade: "", fudanOrder: "" },
},
},
});
}
if (operations.length > 0) {
await DBModel.Hospital.bulkWrite(operations);
await rawCollection.bulkWrite(operations);
}
console.log(